问题描述
它们是等效或相互替代?在任何人去precated如果是哪一个?其中一个是建议在ASP.NET Web应用程序使用。我的目标是从一个特定的目录递归提取所有文件。
Are they equivalent or alternatives to each other? Is any of them deprecated and if so which one? Which one is recommended for use in an ASP.NET web application. My aim is to extract all files from a specific directory recursively.
推荐答案
是提供有关特定目录信息的类的实例。因此,例如,如果你想关于C的信息:\\ TEMP:
Directory is a static class that provides static methods for working with directories. DirectoryInfo is an instance of a class that provides information about a specific directory. So for example if you wanted the information about C:\Temp:
var dirInfo = new DirectoryInfo("C:\\Temp");
if (dirInfo.Exists) {
FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
...
}
如果您只是想的名字作为字符串它可能是更快,更容易避免使用目录的静态方法创建的DirectoryInfo的实例。
If you just wanted the names as strings it might be quicker and easier to avoid creating an instance of DirectoryInfo by using the static methods of Directory.
if (Directory.Exists("C:\\Temp")) {
string[] files = Directory.GetFiles("C:\\Temp", "*.*", SearchOption.AllDirectories);
...
}
总之,这其实并不重要,只要你想要做什么,你使用。也不建议在其他。
In short, it really doesn't matter which you use as long as it does what you want. Neither is recommended over the other.
这篇关于目录VS的DirectoryInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!