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. 因此,若要测试此示例,请在代码中注释掉删除行。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

参数

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.

适用于