DirectoryInfo.Create Metoda

Definicja

Tworzy katalog.

Przeciążenia

Create()

Tworzy katalog.

Create(DirectorySecurity)

Tworzy katalog przy użyciu DirectorySecurity obiektu.

Create()

Tworzy katalog.

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

Wyjątki

Nie można utworzyć katalogu.

Przykłady

Poniższy przykład sprawdza, czy istnieje określony katalog, tworzy katalog, jeśli nie istnieje, i usuwa katalog.

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

Uwagi

Jeśli katalog już istnieje, ta metoda nic nie robi.

Jeśli katalog nie istnieje przed wywołaniem tej metody, wszelkie buforowane informacje o atrybucie o katalogu zostaną opróżnione, jeśli tworzenie zakończy się pomyślnie.

Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.

Zobacz też

Dotyczy

Create(DirectorySecurity)

Tworzy katalog przy użyciu DirectorySecurity obiektu.

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)

Parametry

directorySecurity
DirectorySecurity

Kontrola dostępu do zastosowania do katalogu.

Wyjątki

Obiekt wywołujący nie posiada wymaganych uprawnień.

.NET Framework i .NET Core w wersjach starszych niż 2.1: path jest ciągiem o zerowej długości, zawiera tylko biały odstęp lub zawiera co najmniej jeden nieprawidłowy znak. Możesz wykonać zapytanie o nieprawidłowe znaki przy użyciu GetInvalidPathChars() metody .

path to null.

Podana ścieżka, nazwa pliku lub obie przekraczają maksymalną długość zdefiniowaną przez system.

Określona ścieżka jest nieprawidłowa, na przykład na dysku niezamapowanym.

Tworzenie katalogu z tylko dwukropkiem (:) podjęto próbę znaku.

Przykłady

Poniższy przykład kodu tworzy nowy katalog w folderze tymczasowym użytkownika z określonymi atrybutami zabezpieczeń katalogu:

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);
        }
    }
}

Uwagi

Użyj tego przeciążenia metody, aby utworzyć katalog z kontrolą dostępu, więc nie ma szans, aby można było uzyskać dostęp do katalogu przed zastosowaniem zabezpieczeń.

Jeśli katalog już istnieje, ta metoda nic nie robi.

Aby uzyskać listę typowych zadań we/wy, zobacz Typowe zadania we/wy.

Ważne

Ta metoda została przekierowana do platformy .NET Core 3.1 jako metoda FileSystemAclExtensions rozszerzenia klasy w ramach System.Security.AccessControl zestawu: Create(DirectoryInfo, DirectorySecurity).

Dotyczy