共用方式為


DirectoryInfo.Create 方法

定義

建立目錄。

多載

Create()

建立目錄。

Create(DirectorySecurity)

使用 DirectorySecurity 物件建立目錄。

Create()

來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs

建立目錄。

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

例外狀況

無法建立目錄。

範例

下列範例會檢查指定的目錄是否存在、如果目錄不存在,則會建立目錄,並刪除目錄。

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

備註

除非 的path某個部分無效,否則會建立 中path指定的任何和所有目錄。 參數 path 會指定目錄路徑,而不是檔案路徑。 如果目錄已經存在,這個方法就不會執行任何動作。 如果目錄在呼叫此方法之前不存在,則建立成功時,將會排清有關目錄的任何快取屬性資訊。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

Create(DirectorySecurity)

使用 DirectorySecurity 物件建立目錄。

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)

參數

directorySecurity
DirectorySecurity

要套用至目錄的存取控制。

例外狀況

呼叫端沒有必要的權限。

.NET Framework 和 2.1 之前的 .NET Core 版本:path是長度為零的字串、只包含空格符,或包含一或多個無效字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

pathnull

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

指定的路徑無效,例如位於未對應的磁碟機上。

已嘗試只用冒號 (:) 字元建立目錄

範例

下列程式代碼範例會使用指定的目錄安全性屬性,在使用者的暫存資料夾中建立新的目錄:

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

備註

使用此方法多載建立具有訪問控制的目錄,因此在套用安全性之前,無法存取目錄。

如果目錄已經存在,這個方法就不會執行任何動作。

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

重要

這個方法已移植到 .NET Core 3.1 做為 類別的 FileSystemAclExtensions 擴充方法,做為元件的一部分 System.Security.AccessControlCreate(DirectoryInfo, DirectorySecurity)

適用於