DirectoryInfo.CreateSubdirectory 方法

定義

在指定路徑上建立子目錄。Creates a subdirectory or subdirectories on the specified path. 指定的路徑可以相對於 DirectoryInfo 類別的這個執行個體。The specified path can be relative to this instance of the DirectoryInfo class.

多載

CreateSubdirectory(String)

在指定路徑上建立子目錄。Creates a subdirectory or subdirectories on the specified path. 指定的路徑可以相對於 DirectoryInfo 類別的這個執行個體。The specified path can be relative to this instance of the DirectoryInfo class.

CreateSubdirectory(String, DirectorySecurity)

使用指定的安全性在指定路徑上建立子目錄。Creates a subdirectory or subdirectories on the specified path with the specified security. 指定的路徑可以相對於 DirectoryInfo 類別的這個執行個體。The specified path can be relative to this instance of the DirectoryInfo class.

CreateSubdirectory(String)

在指定路徑上建立子目錄。Creates a subdirectory or subdirectories on the specified path. 指定的路徑可以相對於 DirectoryInfo 類別的這個執行個體。The specified path can be relative to this instance of the DirectoryInfo class.

public:
 System::IO::DirectoryInfo ^ CreateSubdirectory(System::String ^ path);
public System.IO.DirectoryInfo CreateSubdirectory (string path);
member this.CreateSubdirectory : string -> System.IO.DirectoryInfo
Public Function CreateSubdirectory (path As String) As DirectoryInfo

參數

path
String

指定的路徑。The specified path. 不能是不同的磁碟區或通用命名慣例 (UNC) 名稱。This cannot be a different disk volume or Universal Naming Convention (UNC) name.

傳回

path 中指定的最後一個目錄。The last directory specified in path.

例外狀況

path 未指定有效的檔案路徑或包含無效的 DirectoryInfo 字元。path does not specify a valid file path or contains invalid DirectoryInfo characters.

pathnullpath is null.

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

無法建立子目錄。The subdirectory cannot be created.

-或--or- 檔案或目錄已有 path 指定的名稱。A file or directory already has the name specified by path.

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

呼叫端沒有程式碼存取權限,不能建立目錄。The caller does not have code access permission to create the directory.

-或--or- 呼叫端沒有程式碼存取權限,不能讀取傳回的 DirectoryInfo 物件所描述的目錄。The caller does not have code access permission to read the directory described by the returned DirectoryInfo object. path 參數描述現有的目錄時就會發生這種情況。This can occur when the path parameter describes an existing directory.

path 包含不是磁碟機標籤 ("C:\") 一部分的冒號字元 (:)。path contains a colon character (:) that is not part of a drive label ("C:\").

範例

下列範例示範如何建立子目錄。The following example demonstrates creating a subdirectory. 在此範例中,建立後會移除建立的目錄。In this example, the created directories are removed once created. 因此,若要測試這個範例,請將程式碼中的 delete 行批註在一起。Therefore, to test this sample, comment out the delete lines in the code.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Create a reference to a directory.
   DirectoryInfo^ di = gcnew DirectoryInfo( "TempDir" );
   
   // Create the directory only if it does not already exist.
   if ( di->Exists == false )
      di->Create();

   
   // Create a subdirectory in the directory just created.
   DirectoryInfo^ dis = di->CreateSubdirectory( "SubDir" );
   
   // Process that directory as required.
   // ...
   // Delete the subdirectory.
   dis->Delete( true );
   
   // Delete the directory.
   di->Delete( true );
}

using System;
using System.IO;

public class CreateSubTest 
{
    public static void Main() 
    {
        // Create a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Process that directory as required.
        // ...

        // Delete the subdirectory.
        dis.Delete(true);

        // Delete the directory.
        di.Delete(true);
    }
}
Imports System.IO

Public Class CreateSubTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("TempDir")

        ' Create the directory only if it does not already exist.
        If di.Exists = False Then
            di.Create()
        End If

        ' Create a subdirectory in the directory just created.
        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")

        ' Process that directory as required.
        ' ...

        ' Delete the subdirectory.
        dis.Delete(True)

        ' Delete the directory.
        di.Delete(True)
    End Sub
End Class

備註

除非 path 的某些部分無效,否則會建立 path 中指定的任何和所有目錄。Any and all directories specified in path are created, unless some part of path is invalid. path 參數會指定目錄路徑,而不是檔案路徑。The path parameter specifies a directory path, not a file path. 如果子目錄已經存在,這個方法就不會執行任何操作。If the subdirectory already exists, this method does nothing.

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

另請參閱

CreateSubdirectory(String, DirectorySecurity)

使用指定的安全性在指定路徑上建立子目錄。Creates a subdirectory or subdirectories on the specified path with the specified security. 指定的路徑可以相對於 DirectoryInfo 類別的這個執行個體。The specified path can be relative to this instance of the DirectoryInfo class.

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

參數

path
String

指定的路徑。The specified path. 不能是不同的磁碟區或通用命名慣例 (UNC) 名稱。This cannot be a different disk volume or Universal Naming Convention (UNC) name.

directorySecurity
DirectorySecurity

要套用的安全性。The security to apply.

傳回

path 中指定的最後一個目錄。The last directory specified in path.

例外狀況

path 未指定有效的檔案路徑或包含無效的 DirectoryInfo 字元。path does not specify a valid file path or contains invalid DirectoryInfo characters.

pathnullpath is null.

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

無法建立子目錄。The subdirectory cannot be created.

-或--or- 檔案或目錄已有 path 指定的名稱。A file or directory already has the name specified by path.

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

呼叫端沒有程式碼存取權限,不能建立目錄。The caller does not have code access permission to create the directory.

-或--or- 呼叫端沒有程式碼存取權限,不能讀取傳回的 DirectoryInfo 物件所描述的目錄。The caller does not have code access permission to read the directory described by the returned DirectoryInfo object. path 參數描述現有的目錄時就會發生這種情況。This can occur when the path parameter describes an existing directory.

path 包含不是磁碟機標籤 ("C:\") 一部分的冒號字元 (:)。path contains a colon character (:) that is not part of a drive label ("C:\").

備註

除非 path 的某些部分無效,否則會建立 path 中指定的任何和所有目錄。Any and all directories specified in path are created, unless some part of path is invalid. path 參數會指定目錄路徑,而不是檔案路徑。The path parameter specifies a directory path, not a file path. 如果子目錄已經存在,這個方法就不會執行任何操作。If the subdirectory already exists, this method does nothing.

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

適用於