我想获取数组目录中的所有文件(包括子文件夹中的文件)
string[] filePaths = Directory.GetFiles(@"c:\",SearchOption.AllDirectories);
这个问题是:如果抛出异常,整个命令就会停止。有没有更好的方法来做到这一点,如果一个文件夹无法访问它只会跳过它?
答案 0 :(得分:6)
然后你可能需要做更多的打字,并写一个像这样的目录漫步者:
public static string[] FindAllFiles(string rootDir) {
var pathsToSearch = new Queue<string>();
var foundFiles = new List<string>();
pathsToSearch.Enqueue(rootDir);
while (pathsToSearch.Count > 0) {
var dir = pathsToSearch.Dequeue();
try {
var files = Directory.GetFiles(dir);
foreach (var file in Directory.GetFiles(dir)) {
foundFiles.Add(file);
}
foreach (var subDir in Directory.GetDirectories(dir)) {
pathsToSearch.Enqueue(subDir);
}
} catch (Exception /* TODO: catch correct exception */) {
// Swallow. Gulp!
}
}
return foundFiles.ToArray();
}
答案 1 :(得分:0)
Directory.GetFiles不能跳过目录符号链接,而目录符号链接通常会导致循环然后导致异常。
因此,基于@iks的答案和Check if a file is real or a symbolic link,这是一个可以像Directory.EnumerateFiles一样随时提供结果的版本。
public static IEnumerable<string> FindAllFiles(string rootDir)
{
var pathsToSearch = new Queue<string>();
pathsToSearch.Enqueue(rootDir);
while (pathsToSearch.Count > 0)
{
var dir = pathsToSearch.Dequeue();
var foundFiles = new List<string>();
try
{
foreach (var file in Directory.GetFiles(dir))
foundFiles.Add(file);
foreach (var subDir in Directory.GetDirectories(dir))
{
//comment this if want to follow symbolic link
//or follow them conditionally
if (IsSymbolic(subDir)) continue;
pathsToSearch.Enqueue(subDir);
}
}
catch (Exception) {//deal with exceptions here
}
foreach (var file in foundFiles) yield return file;
}
}
static private bool IsSymbolic(string path)
{
FileInfo pathInfo = new FileInfo(path);
return pathInfo.Attributes.HasFlag(System.IO.FileAttributes.ReparsePoint);
}
static public void test()
{
string root = @"D:\root";
foreach (var fn in FindAllFiles(root)
.Where(x=>
true //filter condition here
))
{
Debug.WriteLine(fn);
}
}
答案 2 :(得分:-1)
试试这个:
DirectoryInfo directory = new DirectoryInfo(@"c:\");
DirectoryInfo[] folders = directory.GetDirectories("*", SearchOption.AllDirectories);
List<string> files = new List<string>();
foreach (DirectoryInfo info in folders)
{
foreach (FileInfo file in info.GetFiles())
{
files.Add(file.Name);
}
}
答案 3 :(得分:-1)
或试试这个:
DirectoryInfo dirs = new DirectoryInfo(@"c:\");
List<string> filenames = (from i in dirs.GetFiles("*", SearchOption.AllDirectories)
select i.Name).ToList();
或没有扩展名的文件名:
DirectoryInfo dirs = new DirectoryInfo@"c:\");
List<string> filenames = (from i in dirs.GetFiles("*", SearchOption.AllDirectories)
select System.IO.Path.GetFileNameWithoutExtension(i.Name)).ToList();