DirectoryInfo 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
公开用于创建、移动和枚举目录和子目录的实例方法。 此类不能被继承。
public ref class DirectoryInfo sealed : System::IO::FileSystemInfopublic sealed class DirectoryInfo : System.IO.FileSystemInfo[System.Serializable]
public sealed class DirectoryInfo : System.IO.FileSystemInfo[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DirectoryInfo : System.IO.FileSystemInfotype DirectoryInfo = class
    inherit FileSystemInfo[<System.Serializable>]
type DirectoryInfo = class
    inherit FileSystemInfo[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DirectoryInfo = class
    inherit FileSystemInfoPublic NotInheritable Class DirectoryInfo
Inherits FileSystemInfo- 继承
- 继承
- 属性
示例
下面的示例演示 类的一些main成员DirectoryInfo。
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 the directory 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 the directory 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 the directory 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
以下示例演示如何复制目录及其内容。
using System;
using System.IO;
class CopyDir
{
    public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {
        if (source.FullName.ToLower() == target.FullName.ToLower())
        {
            return;
        }
        // Check if the target directory exists, if not, create it.
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }
        // Copy each file into it's new directory.
        foreach (FileInfo fi in source.GetFiles())
        {
            Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
        }
        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir);
        }
    }
    public static void Main()
    {
        string sourceDirectory = @"c:\sourceDirectory";
        string targetDirectory = @"c:\targetDirectory";
        DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
        DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
        CopyAll(diSource, diTarget);
    }
    // Output will vary based on the contents of the source directory.
}
open System.IO
let rec copyAll (source: DirectoryInfo) (target: DirectoryInfo) =
    if source.FullName.ToLower() <> target.FullName.ToLower() then
        // Check if the target directory exists, if not, create it.
        if not (Directory.Exists target.FullName) then
            Directory.CreateDirectory target.FullName |> ignore
        // Copy each file into it's new directory.
        for fi in source.GetFiles() do
            printfn $@"Copying {target.FullName}\{fi.Name}"
            fi.CopyTo(Path.Combine(string target, fi.Name), true) |> ignore
        // Copy each subdirectory using recursion.
        for diSourceSubDir in source.GetDirectories() do
            target.CreateSubdirectory diSourceSubDir.Name
            |> copyAll diSourceSubDir
let sourceDirectory = @"c:\sourceDirectory"
let targetDirectory = @"c:\targetDirectory"
let diSource = DirectoryInfo sourceDirectory
let diTarget = DirectoryInfo targetDirectory
copyAll diSource diTarget
// Output will vary based on the contents of the source directory.
Imports System.IO
Class CopyDir
    Shared Sub CopyAll(ByVal source As DirectoryInfo, ByVal target As DirectoryInfo)
        If (source.FullName.ToLower() = target.FullName.ToLower()) Then
            Return
        End If
        ' Check if the target directory exists, if not, create it.
        If Directory.Exists(target.FullName) = False Then
            Directory.CreateDirectory(target.FullName)
        End If
        ' Copy each file into it's new directory.
        For Each fi As FileInfo In source.GetFiles()
            Console.WriteLine("Copying {0}\{1}", target.FullName, fi.Name)
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), True)
        Next
        ' Copy each subdirectory using recursion.
        For Each diSourceSubDir As DirectoryInfo In source.GetDirectories()
            Dim nextTargetSubDir As DirectoryInfo = target.CreateSubdirectory(diSourceSubDir.Name)
            CopyAll(diSourceSubDir, nextTargetSubDir)
        Next
    End Sub
    Shared Sub Main()
        Dim sourceDirectory As String = "c:\\sourceDirectory"
        Dim targetDirectory As String = "c:\\targetDirectory"
        Dim diSource As DirectoryInfo = New DirectoryInfo(sourceDirectory)
        Dim diTarget As DirectoryInfo = New DirectoryInfo(targetDirectory)
        CopyAll(diSource, diTarget)
    End Sub
    ' Output will vary based on the contents of the source directory.
End Class
注解
DirectoryInfo将 类用于典型的操作,例如复制、移动、重命名、创建和删除目录。
如果要多次重用对象,请考虑使用 的DirectoryInfo实例方法,而不是类的Directory相应静态方法,因为安全检查并不总是必要的。
注意
在接受路径作为输入字符串的成员中,该路径的格式必须正确,否则将引发异常。 例如,如果路径是完全限定的,但以空格开头,则不会在 类的方法中剪裁该路径。 因此,路径格式不正确,并引发异常。 同样,路径或路径组合不能完全限定两次。 例如,“c:\temp c:\windows”在大多数情况下也会引发异常。 使用接受路径字符串的方法时,请确保路径格式良好。
在接受路径的成员中,路径可以引用文件或仅引用目录。 指定的路径还可以引用服务器和共享名称的相对路径或通用命名约定 (UNC) 路径。 例如,以下所有路径都是可接受的路径:
- C# 中的“c:\\MyDir\\MyFile.txt”或 Visual Basic 中的“c:\MyDir\MyFile.txt”。 
- C# 中的“c:\\MyDir”或 Visual Basic 中的“c:\MyDir”。 
- C# 中的“MyDir\\MySubdir”或 Visual Basic 中的“MyDir\MySubDir”。 
- C# 中的“\\\\MyServer\\MyShare”或 Visual Basic 中的“\\MyServer\MyShare”。 
默认情况下,向所有用户授予对新目录的完全读/写访问权限。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。
构造函数
| DirectoryInfo(String) | 初始化指定路径上的 DirectoryInfo 类的新实例。 | 
字段
| FullPath | 表示目录或文件的完全限定目录。(继承自 FileSystemInfo) | 
| OriginalPath | 最初由用户指定的目录(不论是相对目录还是绝对目录)。(继承自 FileSystemInfo) | 
属性
| Attributes | 获取或设置当前文件或目录的特性。(继承自 FileSystemInfo) | 
| CreationTime | 获取或设置当前文件或目录的创建时间。(继承自 FileSystemInfo) | 
| CreationTimeUtc | 获取或设置当前文件或目录的创建时间,其格式为协调世界时 (UTC)。(继承自 FileSystemInfo) | 
| Exists | 获取指示目录是否存在的值。 | 
| Extension | 获取文件名的扩展名部分,包括前导点  | 
| FullName | 获取目录的完整路径。 | 
| FullName | 获取目录或文件的完整目录。(继承自 FileSystemInfo) | 
| LastAccessTime | 获取或设置上次访问当前文件或目录的时间。(继承自 FileSystemInfo) | 
| LastAccessTimeUtc | 获取或设置上次访问当前文件或目录的时间,其格式为协调世界时 (UTC)。(继承自 FileSystemInfo) | 
| LastWriteTime | 获取或设置上次写入当前文件或目录的时间。(继承自 FileSystemInfo) | 
| LastWriteTimeUtc | 获取或设置上次写入当前文件或目录的时间,其格式为协调世界时 (UTC)。(继承自 FileSystemInfo) | 
| LinkTarget | 获取位于 中的 FullName链接的目标路径,如果  | 
| Name | 获取此 DirectoryInfo 实例的名称。 | 
| Parent | 获取指定的子目录的父目录。 | 
| Root | 获取目录的根部分。 | 
| UnixFileMode | 获取或设置当前文件或目录的 Unix 文件模式。(继承自 FileSystemInfo) | 
方法
扩展方法
| Create(DirectoryInfo, DirectorySecurity) | 创建一个新目录,确保使用指定的目录安全性创建该目录。 如果该目录已存在,则不执行任何操作。 | 
| GetAccessControl(DirectoryInfo) | 返回目录的安全信息。 | 
| GetAccessControl(DirectoryInfo, AccessControlSections) | 返回目录的安全信息。 | 
| SetAccessControl(DirectoryInfo, DirectorySecurity) | 更改现有目录的安全属性。 |