我想在我的文件枚举中过滤出路径C:\$Recycle.bin
。我怎样才能做到这一点?SearchOption.AllDirectories过滤器
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).OrderBy(p => p).ToList();
当我执行上述,我得到下面的错误。
其他信息:访问路径'C:\ $ Recycle.Bin \ S-1-5-21-1600837348-2291285090-976156579-500'被拒绝。
我也想计算每个文件的md5。我有:
var mainDirectory = new DirectoryInfo("\\");
var files = GetDirectories(mainDirectory);
List<string> drives = new List<string>();
foreach (var file in files)
{
//Console.WriteLine(file.Name);
drives.Add(mainDirectory + file.Name);
}
MD5 md5 = MD5.Create();
foreach (string file in drives)
{
// hash path
string relativePath = file.Substring("\\".Length + 1);
byte[] pathBytes = Encoding.UTF8.GetBytes(relativePath.ToLower());
md5.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
// hash contents
try
{
byte[] contentBytes = File.ReadAllBytes(file);
md5.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0);
md5.TransformFinalBlock(contentBytes, 0, contentBytes.Length);
}
catch(UnauthorizedAccessException)
{
continue;
}
catch
{
continue;
}
Console.WriteLine(BitConverter.ToString(md5.Hash).Replace("-", "").ToLower());
}
Console.ReadKey();
恐怕这可能是一个棘手的问题。我认为它是框架中的一个错误。 :( – Gabe
你不能!唯一的选择是自己写一个递归函数 – Silvermind
可能的重复[在搜索文件时访问被拒绝的错误](http://stackoverflow.com/questions/10458862/access-denied-error -in-while-searching-for-files) – Gabe