我正在编写一个控制台应用程序来显示C:\ windows上的文件夹统计信息,并显示其中的总文件,无论如何它可以简化并链接文件类型与用户!这是我到目前为止所得到的:
{
String extention = String.Empty;
// Prompt the user to enter extention type
Console.Write("Please enter extention type: ");
extention = Console.ReadLine();
// This gets the Folder location which in this case is C:\\windows
DirectoryInfo root = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows));
// This is basicly the bit that collects the data after the user has entered the extention type
FileInfo[] executables = root.GetFiles("*exe");
foreach (var exe in executables)
{
//This will show the word txt in the console window
Console.WriteLine(exe.Name);
}
}
}
}
{
String extention2 = String.Empty;
// Prompt the user to enter extention type
extention2 = Console.ReadLine();
// This gets the Folder location which in this case is C:\\windows
DirectoryInfo root2 = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows));
FileInfo[] text = root2.GetFiles("*.txt");
foreach (var txt in text)
{
//This will show the word txt in the console window
Console.WriteLine(txt.Name);
}
}
String extention4 = String.Empty;
// Prompt the user to enter extention type
extention4 = Console.ReadLine();
// This gets the Folder location which in this case is C:\\windows
DirectoryInfo root4 = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows));
FileInfo[] windows = root4.GetFiles("*.win");
foreach (var win in windows)
{
//This will show the word txt in the console window
Console.WriteLine(win.Name);
}
答案 0 :(得分:2)
尝试使用:new DirectoryInfo("C:\\Windows")
如果要获取该目录中的文件列表,请调用:
EnumerateFiles("*",SearchOption.AllDirectories)
DirectoryInfo对象上的
答案 1 :(得分:1)
看看这个:
“如何:获取有关文件,文件夹和驱动器的信息(C#编程指南)”
http://msdn.microsoft.com/en-us/library/6yk7a1b0.aspx
例如:
// Get the files in the directory and print out some information about them.
System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*");
foreach (System.IO.FileInfo fi in fileNames)
{
Console.WriteLine("{0}: {1}: {2}", fi.Name, fi.LastAccessTime, fi.Length);
}
您可以将Console.WriteLine更改为您想要的格式...
<强>更新强>
System.IO.DriveInfo di = new System.IO.DriveInfo(@"C:\");
// Get the root directory
System.IO.DirectoryInfo dirInfo = di.RootDirectory;
// And then you can do .GetFiles()
System.IO.FileInfo[] fileNames = dirInfo.GetFiles("*.*");