DirectoryInfo類是System.IO命名空間中的一個類,用于操作目錄的相關信息和方法。下面是一些DirectoryInfo類的常用方法和屬性的示例用法:
string path = @"C:\MyDirectory";
DirectoryInfo directory = new DirectoryInfo(path);
string fullPath = directory.FullName;
string directoryName = directory.Name;
DirectoryInfo parentDirectory = directory.Parent;
DateTime creationTime = directory.CreationTime;
DateTime lastWriteTime = directory.LastWriteTime;
bool exists = directory.Exists;
directory.Create();
directory.Delete();
FileInfo[] files = directory.GetFiles();
foreach (FileInfo file in files)
{
Console.WriteLine(file.Name);
}
DirectoryInfo[] subDirectories = directory.GetDirectories();
foreach (DirectoryInfo subDirectory in subDirectories)
{
Console.WriteLine(subDirectory.Name);
}
這些只是DirectoryInfo類的一些常用方法和屬性的示例用法,你可以根據自己的需求進一步了解和使用DirectoryInfo類的其他方法和屬性。