DirectoryInfo類是用于操作文件夾和目錄的類,可以用來獲取文件夾的信息、創建文件夾、刪除文件夾等操作。以下是一些有效使用DirectoryInfo類的示例:
DirectoryInfo directory = new DirectoryInfo(@"C:\myfolder");
foreach (var subDirectory in directory.GetDirectories())
{
Console.WriteLine(subDirectory.FullName);
}
foreach (var file in directory.GetFiles())
{
Console.WriteLine(file.FullName);
}
DirectoryInfo directory = new DirectoryInfo(@"C:\newfolder");
directory.Create();
DirectoryInfo directory = new DirectoryInfo(@"C:\folderToDelete");
directory.Delete(true);
DirectoryInfo directory = new DirectoryInfo(@"C:\existingFolder");
if (directory.Exists)
{
Console.WriteLine("Folder exists.");
}
DirectoryInfo directory = new DirectoryInfo(@"C:\myfolder");
DirectoryInfo parentDirectory = directory.Parent;
Console.WriteLine(parentDirectory.FullName);
這些是一些常見的用法,可以根據具體的需求來使用DirectoryInfo類進行文件夾和目錄的操作。