This article is currently in the process of being translated into Chinese (~1% done).
File and directory information
前几章使用的File和Directory类对于直接的文件和目录操作非常好。不过有时还需要获得文件和目录的信息,System.IO名空间再次提供了帮助:使用FileInfo和DirectoryInfo类。本章介绍使用这两个类的一些方法。
FileInfo类
首先看下使用FileInfo类的一个简单方法。
static void Main(string[] args)
{
FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
if(fi != null)
Console.WriteLine(String.Format("Information about file: {0}, {1} bytes, last modified on {2} - Full path: {3}", fi.Name, fi.Length, fi.LastWriteTime, fi.FullName));
Console.ReadKey();
}本示例生成一个FileInfo类的新实例。实例化所需参数为目标文件的路径。此处本可以随意指定一个文件的,但为了活跃一下气氛,直接获取正在运行的本程序不是很酷吗?也就是本代码所在项目编译后的EXE文件。由于控制台程序(也是WinForms包的一部分)不能从应用项目中得到位置信息,此处使用了点反射机制来获得当前执行包的位置。这完全超出了本章范围,不过了解一下也没坏处。
Once we have a FileInfo instance, we output all sorts of information about it. Try running the application and you will see. All very nice and easy, and if you look at the FileInfo class, you will see that it offers even more information, as well as shortcuts to the methods found on the File class - and why not? We have a reference to the file anyway with the FileInfo instance, so we might as well get the same options as on the File class.
The DirectoryInfo class
Now, information about a single file is just fine, but using the DirectoryInfo class, we can get information about all files and directories within a directory, which is obviously a very common scenario. Let me show you with a simple example:
DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
if(di != null)
{
FileInfo[] subFiles = di.GetFiles();
if(subFiles.Length > 0)
{
Console.WriteLine("Files:");
foreach(FileInfo subFile in subFiles)
{
Console.WriteLine(" " + subFile.Name + " (" + subFile.Length + " bytes)");
}
}
Console.ReadKey();
}Instead of a FileInfo instance, we create a DirectoryInfo instance. We use the same trick to get the path of the executing file, and then the GetDirectoryName() method from the Path class, to get the directory part of the path only. We use the GetFiles() method to get an array of FileInfo instances, each representing a file in the directory. We then loop through it, printing out each filename and size.
Perhaps we want the directories as well. It's just as easy:
DirectoryInfo[] subDirs = di.GetDirectories();
if(subDirs.Length > 0)
{
Console.WriteLine("Directories:");
foreach(DirectoryInfo subDir in subDirs)
{
Console.WriteLine(" " + subDir.Name);
}
}In some situations, you might only want files or directories with a specific name or file extension. Fortunately, FileInfo and DirectoryInfo has some pretty good support for that as well.
This will give us all files in the directory with a .exe extension:
FileInfo[] subFiles = di.GetFiles("*.exe");This will give us all the directories which have the word "test" somewhere in the name:
DirectoryInfo[] subDirs = di.GetDirectories("*test*");We can even find both files and directories recursively, which means that it will search in subdirectories of subdirectories of.... the originial directory:
FileInfo[] subFiles = di.GetFiles("*.exe", SearchOption.AllDirectories);To only search the toplevel directory, the code would have to look like this:
FileInfo[] subFiles = di.GetFiles("*.exe", SearchOption.TopDirectoryOnly);Summary
Using the FileInfo and DirectoryInfo classes, we can easily discover information about the file system on the current computer.