Share via


DirectoryInfo.Create Yöntem

Tanım

Bir dizin oluşturur.

Aşırı Yüklemeler

Create()

Bir dizin oluşturur.

Create(DirectorySecurity)

Nesne kullanarak bir DirectorySecurity dizin oluşturur.

Create()

Kaynak:
DirectoryInfo.cs
Kaynak:
DirectoryInfo.cs
Kaynak:
DirectoryInfo.cs

Bir dizin oluşturur.

public:
 void Create();
public void Create ();
member this.Create : unit -> unit
Public Sub Create ()

Özel durumlar

Dizin oluşturulamıyor.

Örnekler

Aşağıdaki örnek, belirtilen bir dizinin var olup olmadığını denetler, yoksa dizini oluşturur ve dizini siler.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Specify the directories you want to manipulate.
   DirectoryInfo^ di = gcnew DirectoryInfo( "c:\\MyDir" );
   try
   {
      
      // Determine whether the directory exists.
      if ( di->Exists )
      {
         
         // Indicate that it already exists.
         Console::WriteLine( "That path exists already." );
         return 0;
      }
      
      // Try to create the directory.
      di->Create();
      Console::WriteLine( "The directory was created successfully." );
      
      // 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 directories you want to manipulate.
        DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");

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

            // Try to create the directory.
            di.Create();
            Console.WriteLine("The directory was created successfully.");

            // 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 directories you want to manipulate.
let di = DirectoryInfo @"c:\MyDir"

try
    // Determine whether the directory exists.
    if di.Exists then
        // Indicate that it already exists.
        printfn "That path exists already."
    else
        // Try to create the directory.
        di.Create()
        printfn "The directory was created successfully."

        // 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 directories you want to manipulate.
        Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
        Try
            ' Determine whether the directory exists.
            If di.Exists Then
                ' Indicate that it already exists.
                Console.WriteLine("That path exists already.")
                Return
            End If

            ' Try to create the directory.
            di.Create()
            Console.WriteLine("The directory was created successfully.")

            '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

Açıklamalar

bir bölümü path geçersiz olmadığı sürece içinde path belirtilen dizinler ve tüm dizinler oluşturulur. path parametresi dosya yolunu değil dizin yolunu belirtir. Dizin zaten varsa, bu yöntem hiçbir şey yapmaz. Bu yöntemi çağırmadan önce dizin yoksa, oluşturma başarılı olursa dizinle ilgili önbelleğe alınmış öznitelik bilgileri temizlenir.

Yaygın G/Ç görevlerinin listesi için bkz. Ortak G/Ç Görevleri.

Ayrıca bkz.

Şunlara uygulanır

Create(DirectorySecurity)

Nesne kullanarak bir DirectorySecurity dizin oluşturur.

public:
 void Create(System::Security::AccessControl::DirectorySecurity ^ directorySecurity);
public void Create (System.Security.AccessControl.DirectorySecurity directorySecurity);
member this.Create : System.Security.AccessControl.DirectorySecurity -> unit
Public Sub Create (directorySecurity As DirectorySecurity)

Parametreler

directorySecurity
DirectorySecurity

Dizine uygulanacak erişim denetimi.

Özel durumlar

Ç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.

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, eşlenmemiş bir sürücüde olmak gibi geçersiz.

Yalnızca iki nokta üst üste (:) karakter) ile dizin oluşturma denendi.

Örnekler

Aşağıdaki kod örneği, belirtilen dizin güvenlik öznitelikleriyle kullanıcının geçici klasörünün içinde yeni bir dizin oluşturur:

using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            DirectorySecurity security = new DirectorySecurity();
            SecurityIdentifier identity = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
            FileSystemAccessRule accessRule = new FileSystemAccessRule(identity, FileSystemRights.FullControl, AccessControlType.Allow);
            security.AddAccessRule(accessRule);
            string path = Path.Combine(Path.GetTempPath(), "directoryToCreate");
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            dirInfo.Create(security);
        }
    }
}

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.

Dizin zaten varsa, bu yöntem hiçbir şey yapmaz.

Yaygın G/Ç görevlerinin listesi için bkz. Ortak G/Ç Görevleri.

Önemli

Bu yöntem, derlemenin bir parçası olarak sınıfının bir uzantı yöntemi FileSystemAclExtensions olarak .NET Core 3.1'e System.Security.AccessControl taşınabilir: Create(DirectoryInfo, DirectorySecurity).

Şunlara uygulanır