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类进行文件夹和目录的操作。