DirectoryInfo.Create DirectoryInfo.Create DirectoryInfo.Create DirectoryInfo.Create Method

定義

建立目錄。Creates a directory.

多載

Create() Create() Create() Create()

建立目錄。Creates a directory.

Create(DirectorySecurity) Create(DirectorySecurity) Create(DirectorySecurity)

使用 DirectorySecurity 物件建立目錄Creates a directory using a DirectorySecurity object.

Create() Create() Create() Create()

建立目錄。Creates a directory.

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

例外狀況

無法建立目錄The directory cannot be created.

範例

下列範例會檢查指定的目錄是否存在、建立不存在的目錄,以及刪除目錄。The following example checks whether a specified directory exists, creates the directory if it does not exist, and deletes the directory.

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 {}
    }
}
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

備註

如果目錄已經存在,這個方法不會執行任何操作。If the directory already exists, this method does nothing.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
用於寫入檔案。for writing files. 相關聯的列舉:WriteAssociated enumeration: Write

另請參閱

Create(DirectorySecurity) Create(DirectorySecurity) Create(DirectorySecurity)

使用 DirectorySecurity 物件建立目錄Creates a directory using a DirectorySecurity object.

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

參數

directorySecurity
DirectorySecurity DirectorySecurity DirectorySecurity DirectorySecurity

要套用至目錄的存取控制。The access control to apply to the directory.

例外狀況

path 指定的目錄是唯讀或不是空的。The directory specified by path is read-only or is not empty.

呼叫端沒有必要的使用權限。The caller does not have the required permission.

path 為零長度字串,只包含空格,或包含一或多個如 InvalidPathChars 所定義的無效字元。path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

指定的路徑無效,例如位於未對應的磁碟機上。The specified path is invalid, such as being on an unmapped drive.

已嘗試只用冒號 (:) 字元建立目錄Creating a directory with only the colon (:) character was attempted.

備註

使用這個方法多載來建立具有存取控制的目錄,如此一來,在套用安全性之前,就無法存取目錄。Use this method overload to create a directory with access control, so there is no chance the directory can be accessed before security is applied.

如果目錄已經存在,這個方法不會執行任何操作。If the directory already exists, this method does nothing.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
以取得建立目錄的許可權。for permission to create a directory. 安全性動作: DemandSecurity action: Demand. 相關聯的列舉:ReadAssociated enumeration: Read

適用於