Directory.CreateDirectory Yöntem

Tanım

Belirtilen yolda tüm dizinleri oluşturur.

Aşırı Yüklemeler

CreateDirectory(String)

Belirtilen yolda zaten varolmadıkları sürece tüm dizinleri ve alt dizinleri oluşturur.

CreateDirectory(String, UnixFileMode)

Belirtilen yolda belirtilen izinlere sahip tüm dizinleri ve alt dizinleri, önceden mevcut olmadıkları sürece oluşturur.

CreateDirectory(String, DirectorySecurity)

Belirtilen yolda, belirtilen Windows güvenliğini uygulayarak, önceden mevcut olmadıkları sürece tüm dizinleri oluşturur.

CreateDirectory(String)

Kaynak:
Directory.cs
Kaynak:
Directory.cs
Kaynak:
Directory.cs

Belirtilen yolda zaten varolmadıkları sürece tüm dizinleri ve alt dizinleri oluşturur.

public:
 static System::IO::DirectoryInfo ^ CreateDirectory(System::String ^ path);
public static System.IO.DirectoryInfo CreateDirectory (string path);
static member CreateDirectory : string -> System.IO.DirectoryInfo
Public Shared Function CreateDirectory (path As String) As DirectoryInfo

Parametreler

path
String

Oluşturulacak dizin.

Döndürülenler

Belirtilen yolda dizini temsil eden bir nesne. Belirtilen yoldaki bir dizinin zaten var olup olmadığına bakılmaksızın bu nesne döndürülür.

Özel durumlar

tarafından path belirtilen dizin bir dosyadır.

-veya-

Ağ adı bilinmiyor.

Çağıranın gerekli izni yok.

2.1'den eski .NET Framework ve .NET Core sürümleri: path sıfır uzunlukta bir dizedir, yalnızca boşluk içerir veya bir veya daha fazla geçersiz karakter içerir. yöntemini kullanarak GetInvalidPathChars() geçersiz karakterleri sorgulayabilirsiniz.

-veya-

path yalnızca iki nokta üst üste karakteri (:)) ile önek olarak eklenir veya karakter içerir.

path, null değeridir.

Belirtilen yol, dosya adı veya her ikisi birden sistem tarafından tanımlanan en fazla uzunluğu aşıyor.

Belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).

path bir sürücü etiketinin parçası olmayan iki nokta üst üste karakteri (:) ("C:\") içerir.

Örnekler

Aşağıdaki örnek belirtilen dizini oluşturur ve siler:

using namespace System;
using namespace System::IO;
int main()
{
   
   // Specify the directory you want to manipulate.
   String^ path = "c:\\MyDir";
   try
   {
      
      // Determine whether the directory exists.
      if ( Directory::Exists( path ) )
      {
         Console::WriteLine( "That path exists already." );
         return 0;
      }
      
      // Try to create the directory.
      DirectoryInfo^ di = Directory::CreateDirectory( path );
      Console::WriteLine( "The directory was created successfully at {0}.", Directory::GetCreationTime( path ) );
      
      // Delete the directory.
      di->Delete();
      Console::WriteLine( "The directory was deleted successfully." );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }

}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        // Specify the directory you want to manipulate.
        string path = @"c:\MyDir";

        try
        {
            // Determine whether the directory exists.
            if (Directory.Exists(path))
            {
                Console.WriteLine("That path exists already.");
                return;
            }

            // Try to create the directory.
            DirectoryInfo di = Directory.CreateDirectory(path);
            Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));

            // Delete the directory.
            di.Delete();
            Console.WriteLine("The directory was deleted successfully.");
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        finally {}
    }
}
open System.IO

// Specify the directory you want to manipulate.
let path = @"c:\MyDir"

try
    // Determine whether the directory exists.
    if Directory.Exists path then
        printfn "That path exists already."
    else
        // Try to create the directory.
        let di = Directory.CreateDirectory path
        printfn $"The directory was created successfully at {Directory.GetCreationTime path}."

        // Delete the directory.
        di.Delete()
        printfn "The directory was deleted successfully."
with e ->
    printfn $"The process failed: {e}"
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directory you want to manipulate.
        Dim path As String = "c:\MyDir"

        Try
            ' Determine whether the directory exists.
            If Directory.Exists(path) Then
                Console.WriteLine("That path exists already.")
                Return
            End If

            ' Try to create the directory.
            Dim di As DirectoryInfo = Directory.CreateDirectory(path)
            Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path))

            ' Delete the directory.
            di.Delete()
            Console.WriteLine("The directory was deleted successfully.")

        Catch e As Exception
            Console.WriteLine("The process failed: {0}.", e.ToString())
        End Try
    End Sub
End Class

Geçerli dizin C:\Users\User1 olduğunda C:\Users\User1\Public\Html dizinini oluşturmak için, ters eğik çizginin düzgün yorumlandığından emin olmak için aşağıdaki çağrılardan herhangi birini kullanın:

Directory.CreateDirectory("Public\Html")
Directory.CreateDirectory("\Users\User1\Public\Html")
Directory.CreateDirectory("c:\Users\User1\Public\Html")
Directory.CreateDirectory("Public\\Html");
Directory.CreateDirectory("\\Users\\User1\\Public\\Html");
Directory.CreateDirectory("c:\\Users\\User1\\Public\\Html");
Directory::CreateDirectory("Public\\Html");
Directory::CreateDirectory("\\Users\\User1\\Public\\Html");
Directory::CreateDirectory("c:\\Users\\User1\\Public\\Html");

Açıklamalar

içinde belirtilen path dizinler ve tüm dizinler, henüz mevcut olmadıkları veya bir bölümü path geçersiz olmadığı sürece oluşturulur. Dizin zaten varsa, bu yöntem yeni bir dizin oluşturmaz, ancak var olan dizin için bir DirectoryInfo nesne döndürür.

path parametresi dosya yolunu değil dizin yolunu belirtir.

Dizin oluşturulmadan önce parametrenin path sonundan sondaki boşluklar kaldırılır.

Yazma erişiminiz olan bir paylaşımda, uzak bir bilgisayarda dizin oluşturabilirsiniz. UNC yolları desteklenir; örneğin, için pathaşağıdakileri belirtebilirsiniz: \\2009\Archives\December Visual Basic'te ve \\\\2009\\Archives\\December C# dilinde.

Yalnızca iki nokta üst üste karakteriyle dizin oluşturma (:) desteklenmez ve bir NotSupportedException oluşturulmasına neden olur.

Unix sistemlerinde yol ayırıcı olarak eğik çizgi (/) kullanın.

Ayrıca bkz.

Şunlara uygulanır

CreateDirectory(String, UnixFileMode)

Kaynak:
Directory.cs
Kaynak:
Directory.cs
Kaynak:
Directory.cs

Belirtilen yolda belirtilen izinlere sahip tüm dizinleri ve alt dizinleri, önceden mevcut olmadıkları sürece oluşturur.

public:
 static System::IO::DirectoryInfo ^ CreateDirectory(System::String ^ path, System::IO::UnixFileMode unixCreateMode);
[System.Runtime.Versioning.UnsupportedOSPlatform("windows")]
public static System.IO.DirectoryInfo CreateDirectory (string path, System.IO.UnixFileMode unixCreateMode);
[<System.Runtime.Versioning.UnsupportedOSPlatform("windows")>]
static member CreateDirectory : string * System.IO.UnixFileMode -> System.IO.DirectoryInfo
Public Shared Function CreateDirectory (path As String, unixCreateMode As UnixFileMode) As DirectoryInfo

Parametreler

path
String

Oluşturulacak dizin.

unixCreateMode
UnixFileMode

Dizin oluşturmak için kullanılan Unix dosya modunu belirten numaralandırma değerlerinin bit düzeyinde birleşimi.

Döndürülenler

Belirtilen yolda dizini temsil eden bir nesne. Belirtilen yoldaki bir dizinin zaten var olup olmadığına bakılmaksızın bu nesne döndürülür.

Öznitelikler

Özel durumlar

path sıfır uzunlukta bir dizedir veya bir veya daha fazla geçersiz karakter içerir. yöntemini kullanarak GetInvalidPathChars() geçersiz karakterleri sorgulayabilirsiniz.

path, null değeridir.

Dosya modu geçersiz.

Çağıranın gerekli izni yok.

Belirtilen yol, sistem tarafından tanımlanan uzunluk üst sınırını aşıyor.

path bir dosyadır.

öğesinin path bir bileşeni bir dizin değildir.

Şunlara uygulanır

CreateDirectory(String, DirectorySecurity)

Belirtilen yolda, belirtilen Windows güvenliğini uygulayarak, önceden mevcut olmadıkları sürece tüm dizinleri oluşturur.

public:
 static System::IO::DirectoryInfo ^ CreateDirectory(System::String ^ path, System::Security::AccessControl::DirectorySecurity ^ directorySecurity);
public static System.IO.DirectoryInfo CreateDirectory (string path, System.Security.AccessControl.DirectorySecurity directorySecurity);
static member CreateDirectory : string * System.Security.AccessControl.DirectorySecurity -> System.IO.DirectoryInfo
Public Shared Function CreateDirectory (path As String, directorySecurity As DirectorySecurity) As DirectoryInfo

Parametreler

path
String

Oluşturulacak dizin.

directorySecurity
DirectorySecurity

Dizine uygulanacak erişim denetimi.

Döndürülenler

Belirtilen yolda dizini temsil eden bir nesne. Belirtilen yoldaki bir dizinin zaten var olup olmadığına bakılmaksızın bu nesne döndürülür.

Özel durumlar

tarafından path belirtilen dizin bir dosyadır.

-veya-

Ağ adı bilinmiyor.

Çağıranın gerekli izni yok.

2.1'den eski .NET Framework ve .NET Core sürümleri: path sıfır uzunlukta bir dizedir, yalnızca boşluk içerir veya bir veya daha fazla geçersiz karakter içerir. yöntemini kullanarak GetInvalidPathChars() geçersiz karakterleri sorgulayabilirsiniz.

-veya-

path yalnızca iki nokta üst üste karakteri (:)) ile önek olarak eklenir veya karakter içerir.

path, null değeridir.

Belirtilen yol, dosya adı veya her ikisi birden sistem tarafından tanımlanan en fazla uzunluğu aşıyor.

Belirtilen yol geçersiz (örneğin, eşlenmemiş bir sürücüde).

path bir sürücü etiketinin parçası olmayan iki nokta üst üste karakteri (:) ("C:\") içerir.

Örnekler

Aşağıdaki örnek, iki kullanıcı hesabı için erişim kurallarıyla yeni bir dizin oluşturur.

using System;
using System.IO;
using System.Security.AccessControl;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectorySecurity securityRules = new DirectorySecurity();
            securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\account1", FileSystemRights.Read, AccessControlType.Allow));
            securityRules.AddAccessRule(new FileSystemAccessRule(@"Domain\account2", FileSystemRights.FullControl, AccessControlType.Allow));

            DirectoryInfo di = Directory.CreateDirectory(@"C:\destination\NewDirectory", securityRules);
        }
    }
}
open System.IO
open System.Security.AccessControl

let securityRules = DirectorySecurity()
securityRules.AddAccessRule(FileSystemAccessRule(@"Domain\account1", FileSystemRights.Read, AccessControlType.Allow))
securityRules.AddAccessRule(FileSystemAccessRule(@"Domain\account2", FileSystemRights.FullControl, AccessControlType.Allow))

let di = Directory.CreateDirectory(@"C:\destination\NewDirectory", securityRules)
Imports System.IO
Imports System.Security.AccessControl

Module Module1

    Sub Main()
        Dim securityRules As DirectorySecurity = New DirectorySecurity()
        securityRules.AddAccessRule(New FileSystemAccessRule("Domain\account1", FileSystemRights.Read, AccessControlType.Allow))
        securityRules.AddAccessRule(New FileSystemAccessRule("Domain\account2", FileSystemRights.FullControl, AccessControlType.Allow))

        Dim di As DirectoryInfo = Directory.CreateDirectory("C:\destination\NewDirectory", securityRules)
    End Sub

End Module

Açıklamalar

Erişim denetimine sahip bir dizin oluşturmak için bu yöntem aşırı yüklemesini kullanın, bu nedenle güvenlik uygulanmadan önce dizine erişilebilme olasılığı yoktur.

Parametresinde path belirtilen dizinler ve tüm dizinler, zaten mevcut olmadıkları veya bir bölümü path geçersiz olmadığı sürece oluşturulur. path parametresi dosya yolunu değil dizin yolunu belirtir. Dizin zaten varsa, bu yöntem yeni bir dizin oluşturmaz, ancak var olan dizin için bir DirectoryInfo nesne döndürür.

Dizin oluşturulmadan önce parametrenin path sonundan sondaki boşluklar kaldırılır.

Yazma erişiminiz olan bir paylaşımda, uzak bir bilgisayarda dizin oluşturabilirsiniz. UNC yolları desteklenir; örneğin, için pathaşağıdakileri belirtebilirsiniz: \\2009\Archives\December Visual Basic'te ve \\\\2009\\Archives\\December C# dilinde.

Yalnızca iki nokta karakteriyle dizin oluşturma (:) desteklenmez ve bir NotSupportedException oluşturulmasına neden olur.

Şunlara uygulanır