题
有没有一种方法可以查找特定类型的文件数量,而不必循环遍历 Directory.GetFiles() 或类似方法中的所有结果?我正在寻找这样的东西:
int ComponentCount = MagicFindFileCount(@"c:\windows\system32", "*.dll");
我知道我可以创建一个递归函数来调用 Directory.GetFiles ,但如果我可以在不进行所有迭代的情况下执行此操作,那么它会更干净。
编辑: 如果不递归和迭代就不可能做到这一点,那么最好的方法是什么?
解决方案
您应该使用 Directory.GetFiles(路径、搜索模式、搜索选项) Directory.GetFiles() 的重载。
Path 指定路径,searchPattern 指定通配符(例如,*、*.format),SearchOption 提供包含子目录的选项。
此搜索的返回数组的 Length 属性将为您的特定搜索模式和选项提供正确的文件计数:
string[] files = directory.GetFiles(@"c:\windows\system32", "*.dll", SearchOption.AllDirectories);
return files.Length;
编辑: 或者你可以使用 Directory.EnumerateFiles 方法
return Directory.EnumerateFiles(@"c:\windows\system32", "*.dll", SearchOption.AllDirectories).Count();
其他提示
最灵活的方法是使用 linq:
var fileCount = (from file in Directory.EnumerateFiles(@"H:\iPod_Control\Music", "*.mp3", SearchOption.AllDirectories)
select file).Count();
您可以使用 GetFiles 的此重载:
以及 SearchOption 的这个成员:
所有目录 - 包括搜索操作中的当前目录和所有子目录。此选项包括搜索中的已安装驱动器和符号链接的再现点。
GetFiles 返回一个字符串数组,因此您可以获取长度,即找到的文件数。
我一直在寻找更优化的版本。由于我还没有找到它,所以我决定编码并在这里分享:
public static int GetFileCount(string path, string searchPattern, SearchOption searchOption)
{
var fileCount = 0;
var fileIter = Directory.EnumerateFiles(path, searchPattern, searchOption);
foreach (var file in fileIter)
fileCount++;
return fileCount;
}
所有使用 GetFiles/GetDirectories 的解决方案都有点慢,因为需要创建所有这些对象。使用枚举,它不会创建任何临时对象(FileInfo/DirectoryInfo)。
见备注 http://msdn.microsoft.com/en-us/library/dd383571.aspx 了解更多信息
使用递归,您的 MagicFindFileCount 将如下所示:
private int MagicFindFileCount( string strDirectory, string strFilter ) {
int nFiles = Directory.GetFiles( strDirectory, strFilter ).Length;
foreach( String dir in Directory.GetDirectories( strDirectory ) ) {
nFiles += GetNumberOfFiles(dir, strFilter);
}
return nFiles;
}
尽管 乔恩的解决方案 可能是更好的一个。
我有一个应用程序,可以生成父目录中的目录和文件的计数。有些目录包含数千个子目录,每个子目录中有数千个文件。为了在保持响应式用户界面的同时做到这一点,我执行以下操作(将路径发送到 已选择目录路径 方法):
public class DirectoryFileCounter
{
int mDirectoriesToRead = 0;
// Pass this method the parent directory path
public void ADirectoryPathWasSelected(string path)
{
// create a task to do this in the background for responsive ui
// state is the path
Task.Factory.StartNew((state) =>
{
try
{
// Get the first layer of sub directories
this.AddCountFilesAndFolders(state.ToString())
}
catch // Add Handlers for exceptions
{}
}, path));
}
// This method is called recursively
private void AddCountFilesAndFolders(string path)
{
try
{
// Only doing the top directory to prevent an exception from stopping the entire recursion
var directories = Directory.EnumerateDirectories(path, "*.*", SearchOption.TopDirectoryOnly);
// calling class is tracking the count of directories
this.mDirectoriesToRead += directories.Count();
// get the child directories
// this uses an extension method to the IEnumerable<V> interface,
// which will run a function on an object. In this case 'd' is the
// collection of directories
directories.ActionOnEnumerable(d => AddCountFilesAndFolders(d));
}
catch // Add Handlers for exceptions
{
}
try
{
// count the files in the directory
this.mFilesToRead += Directory.EnumerateFiles(path).Count();
}
catch// Add Handlers for exceptions
{ }
}
}
// Extension class
public static class Extensions
{
// this runs the supplied method on each object in the supplied enumerable
public static void ActionOnEnumerable<V>(this IEnumerable<V> nodes,Action<V> doit)
{
foreach (var node in nodes)
{
doit(node);
}
}
}
必须有人来做迭代部分。
AFAIK,.NET 中还没有这样的方法,所以我想有人必须是你。