📅  最后修改于: 2020-10-31 04:19:32             🧑  作者: Mango
DirectoryInfo类是System.IO名称空间的一部分。它用于创建,删除和移动目录。它提供了执行与目录和子目录相关的操作的方法。它是一个密封的类,因此我们不能继承它。
DirectoryInfo类提供了下面列出的构造函数,方法和属性。
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class DirectoryInfo : FileSystemInfo
下表包含DirectoryInfo类的构造函数。
Constructor | Description |
---|---|
DirectoryInfo(String) | It is used to initialize a new instance of the DirectoryInfo class on the specified path. |
下表包含DirectoryInfo类的属性。
Property | Description |
---|---|
Attributes | It is used to get or set the attributes for the current file or directory. |
CreationTime | It is used to get or set the creation time of the current file or directory. |
CreationTimeUtc | It is used to get or set creation time, in coordinated universal time (UTC). |
Exists | It is used to get a value indicating whether the directory exists. |
Extension | It is used to get the string representing the extension part of the file. |
FullName | It is used to get the full path of the directory. |
LastAccessTime | It is used to get or set the time the current file or directory was last accessed. |
LastAccessTimeUtc | It is used to get or set the time, in coordinated universal time (UTC) that the current file or directory was last accessed. |
LastWriteTime | It is used to get or set the time when the current file or directory was last written. |
LastWriteTimeUtc | It is used to get or set the time, in coordinated universal time (UTC), when the current file or directory was last written. |
Name | It is used to get the name of this DirectoryInfo instance. |
Parent | It is used to get the parent directory of a specified subdirectory. |
Root | It is used to get the root portion of the directory. |
下表包含DirectoryInfo类的方法。
Method | Description |
---|---|
Create() | It is used to create a directory. |
Create(DirectorySecurity) | It is used to create a directory using a DirectorySecurity object. |
CreateObjRef(Type) | It is used to create an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. |
CreateSubdirectory(String) | It is used to create a subdirectory or subdirectories on the specified path. |
CreateSubdirectory(String,DirectorySecurity) | It is used to create a subdirectory or subdirectories on the specified path with the specified security. |
Delete() | It is used to delete this DirectoryInfo if it is empty. |
Delete(Boolean) | It is used to delete this instance of a DirectoryInfo, specifying whether to delete subdirectories and files. |
EnumerateDirectories() | It returns an enumerable collection of directory information in the current directory. |
EnumerateFiles() | It returns an enumerable collection of file information in the current directory. |
GetAccessControl() | It is used to get a DirectorySecurity object that encapsulates the access control list (ACL) entries for the directory. |
GetDirectories() | It returns the subdirectories of the current directory. |
GetFiles() | It returns a file list from the current directory. |
GetType() | It is used to get the Type of the current instance. |
MoveTo(String) | It is used to move a DirectoryInfo instance and its contents to a new path. |
Refresh() | It is used to refresh the state of the object. |
SetAccessControl(DirectorySecurity) | It is used to set access control list (ACL) entries described by a DirectorySecurity object. |
ToString() | It returns the original path that was passed by the user. |
在以下示例中,我们通过指定目录路径来创建javatpoint目录。
using System;
using System.IO;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
// Provide directory name with complete location.
DirectoryInfo directory = new DirectoryInfo(@"F:\javatpoint");
try
{
// Check, directory exist or not.
if (directory.Exists)
{
Console.WriteLine("Directory already exist.");
return;
}
// Creating a new directory.
directory.Create();
Console.WriteLine("The directory is created successfully.");
}
catch (Exception e)
{
Console.WriteLine("Directory not created: {0}", e.ToString());
}
}
}
}
输出:
The directory is created successfully.
在下面的屏幕截图中,我们可以看到目录已创建。
DirectoryInfo类还提供了一个delete方法来删除创建的目录。在以下程序中,我们将删除在上一个程序中创建的目录。
using System;
using System.IO;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
// Providing directory name with complete location.
DirectoryInfo directory = new DirectoryInfo(@"F:\javatpoint");
try
{
// Deleting directory
directory.Delete();
Console.WriteLine("The directory is deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine("Something went wrong: {0}", e.ToString());
}
}
}
}
输出:
The directory is deleted successfully.
如果指定的目录不存在,则抛出System.IO.DirectoryNotFoundException异常。