System.IO.DirectoryInfo.CreateSubdirectory 方法 (String)

方法描述

在指定路径中创建一个或多个子目录。 指定路径可以是相对于 DirectoryInfo 类的此实例的路径。

语法定义(C# System.IO.DirectoryInfo.CreateSubdirectory 方法 (String) 的用法)

public DirectoryInfo CreateSubdirectory(
	string path
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
path System-String 指定的路径。它不能是另一个磁盘卷或“统一命名约定”(UNC) 名称。
返回值 System.IO.DirectoryInfo 在 path 中指定的最后一个目录。

提示和注释

除非 path 的某些部分无效,否则,将创建在 path 中指定的所有目录。 path 参数指定目录路径,而不是文件路径。 如果子目录已经存在,则此方法不执行任何操作。

注意

路径名称限制为 248 个字符。

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

System.IO.DirectoryInfo.CreateSubdirectory 方法 (String)例子

因此,要测试该示例,应在代码中注释掉删除行。

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

异常

异常 异常描述
ArgumentException path 未指定有效文件路径或包含无效的 DirectoryInfo 字符。
ArgumentNullException path 为 null。
DirectoryNotFoundException 指定的路径无效,比如在未映射的驱动器上。
IOException
  • 不能创建子目录。
  • 已有文件或目录具有 path 所指定的名称。
PathTooLongException 指定的路径、文件名或者两者都超出了系统定义的最大长度。 例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。 指定的路径、文件名或两者都太长。
SecurityException
  • 调用方不具有创建目录的代码访问权限。
  • 调用方不具有读取返回的 DirectoryInfo 对象描述的目录的代码访问权限。 这可能在 path 参数描述现有目录时发生。
NotSupportedException path 包含一个冒号字符 (:),该冒号字符不是驱动器标签(“C:\”)的一部分。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。