DirectoryInfo 类
公开用于创建、移动和枚举目录和子目录的实例方法。无法继承此类。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class DirectoryInfo
Inherits FileSystemInfo
用法
Dim instance As DirectoryInfo
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class DirectoryInfo : FileSystemInfo
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class DirectoryInfo sealed : public FileSystemInfo
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public final class DirectoryInfo extends FileSystemInfo
SerializableAttribute
ComVisibleAttribute(true)
public final class DirectoryInfo extends FileSystemInfo
备注
将 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 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
重命名或移动目录。 |
|
删除目录。 |
|
创建目录。 |
|
创建子目录。 |
|
查看目录中的文件。 |
|
查看目录的子目录。 |
|
查看目录中的所有文件和所有子目录。 |
|
查看目录大小。 |
Directory |
确定文件是否存在。 |
|
按大小对目录中的文件排序。 |
GetFileSystemInfos |
确定目录是否存在。 |
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台说明: 因为各设备的文件系统具有不同的操作方式,因此 .NET Compact Framework 不支持获取或设置目录属性。
示例
下面的示例演示了 DirectoryInfo 类的某些主要成员。
Imports System
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 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 {}
}
}
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 );
}
}
import System.*;
import System.IO.*;
class Test
{
public static void main(String[] args)
{
// Specify the directories you want to manipulate.
DirectoryInfo di = new DirectoryInfo("c:\\MyDir");
try {
// Determine whether the directory exists.
if (di.get_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 (System.Exception e) {
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {
}
} //main
} //Test
下面的示例演示如何复制目录及其内容。
继承层次结构
System.Object
System.MarshalByRefObject
System.IO.FileSystemInfo
System.IO.DirectoryInfo
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
DirectoryInfo 成员
System.IO 命名空间
File
Attributes
Directory 类
Path