在目录中search特定的文件名

我试图search系统上所有卷上的特定文件名,除了系统卷信息或Windows目录,并打印出每个文件的每个完整path。

我试图使用Directory.GetFiles ,但它只search内驱动器C:\或驱动器D:\(取决于哪个驱动器指定)

我怎样才能达到我的目标?

例如,我想运行程序并search名称为“.DS_Store”的文件(用于使用mac,现在我的文件系统中充满了这些文件….)

我真的很感激任何帮助。 先谢谢你

UPD。 程序应该使用pipe理员权限执行

Solutions Collecting From Web of "在目录中search特定的文件名"

作为cryptolocker分析的一部分,我写了一个定位目标扩展的原型,下面是如何使用它

 var cmd = new LocateTargetFilesCommand(); cmd.Execute() 

它将搜索所有驱动器的两个扩展名: .xlsx.doc 。 最大的问题是处理权限问题。 此外,子文件夹可能具有更广泛的权限,因此如果您无法访问父文件夹,仍然可以访问其根目录。

最后, cmd.FoundTargets将包含所需文件扩展名的绝对路径

 public class LocateTargetFilesCommand { private string[] _targetExtensions = new[] { "xlsx", "doc" }; public LocateTargetFilesCommand() { FoundTargets = new ConcurrentQueue<string>(); DirsToSearch = new List<string>(); } public ConcurrentQueue<string> FoundTargets { get; private set; } public List<string> DirsToSearch { get; private set; } public void Execute() { DriveInfo[] driveInfos = DriveInfo.GetDrives(); Console.WriteLine("Start searching"); foreach (var driveInfo in driveInfos) { if (!driveInfo.IsReady) continue; Console.WriteLine("Locating directories for drive: " + driveInfo.RootDirectory); GetAllDirs(driveInfo.RootDirectory.ToString()); Console.WriteLine("Found {0} folders", DirsToSearch.Count); foreach (var ext in _targetExtensions) { Console.WriteLine("Searching for .*" + ext); int currentNotificationProgress = 0; for (int i = 0; i < DirsToSearch.Count; i++) { int progressPercentage = (i * 100) / DirsToSearch.Count; if (progressPercentage != 0) { if (progressPercentage % 10 == 0 && currentNotificationProgress != progressPercentage) { Console.WriteLine("Completed {0}%", progressPercentage); currentNotificationProgress = progressPercentage; } } var dir = DirsToSearch[i]; try { string[] files = Directory.GetFiles(dir, "*." + ext, SearchOption.TopDirectoryOnly); foreach (var file in files) { FoundTargets.Enqueue(file); } } catch (UnauthorizedAccessException ex) { Console.WriteLine("Skipping directory: {0}", dir); DirsToSearch.Remove(dir); } } Console.WriteLine("So far located {0} targets", FoundTargets.Count); } DirsToSearch.Clear(); } Console.WriteLine("Stop searching"); } public void GetAllDirs(string root) { try { string[] dirs = Directory.GetDirectories(root); DirsToSearch.AddRange(dirs); foreach (var dir in dirs) { GetAllDirs(dir); } } catch (UnauthorizedAccessException) { //swallow } } } 

使用GetLogicalDrives

 foreach (var d in Directory.GetLogicalDrives()) { var files = Directory.GetFiles(d, file); } 

尝试这个:

 foreach (var drive in Directory.GetLogicalDrives()) { foreach (var fileInfo in from dir in Directory.GetDirectories(drive) from file in Directory.GetFiles(dir) select new FileInfo(file)) { // get all details in fileInfo } } 

Thx到oleksii ,但这里是我如何使用此代码来排除所有系统目录

 public class LocateTargetFilesCommand { private string[] _targetExtensions = new[] { ".DS_Store" }; public LocateTargetFilesCommand() { FoundTargets = new ConcurrentQueue<string>(); DirsToSearch = new List<string>(); } public ConcurrentQueue<string> FoundTargets { get; private set; } public List<string> DirsToSearch { get; private set; } public void Execute() { DriveInfo[] driveInfos = DriveInfo.GetDrives(); Console.WriteLine("Start searching"); foreach (var driveInfo in driveInfos) { if (!driveInfo.IsReady) continue; Console.WriteLine("Locating directories for drive: " + driveInfo.RootDirectory); GetAllDirs(driveInfo.RootDirectory.ToString()); Console.WriteLine("Found {0} folders", DirsToSearch.Count); foreach (var ext in _targetExtensions) { Console.WriteLine("Searching for " + ext); int currentNotificationProgress = 0; for (int i = 0; i < DirsToSearch.Count; i++) { int progressPercentage = (i * 100) / DirsToSearch.Count; if (progressPercentage != 0) { if (progressPercentage % 10 == 0 && currentNotificationProgress != progressPercentage) { Console.WriteLine("Completed {0}%", progressPercentage); currentNotificationProgress = progressPercentage; } } var dir = DirsToSearch[i]; try { string[] files = Directory.GetFiles(dir, ext, SearchOption.TopDirectoryOnly); foreach (var file in files) { FoundTargets.Enqueue(file); } } catch (UnauthorizedAccessException ex) { Console.WriteLine("Skipping directory: {0}", dir); DirsToSearch.Remove(dir); } } Console.WriteLine("So far located {0} targets", FoundTargets.Count); } DirsToSearch.Clear(); } Console.WriteLine("Stop searching"); } public void GetAllDirs(string root) { try { string[] dirs = Directory.GetDirectories(root); DirsToSearch.AddRange(dirs); foreach (var dir in dirs) { if(dir.Contains("Windows")) Console.WriteLine("Skipped Windows Directory"); else if (dir.Contains("Users")) Console.WriteLine("Skipped Users Directory"); else if (dir.Contains("MSOCache")) Console.WriteLine("Skipped MSOCache Directory"); else if (dir.Contains("SYSTEM.SAV")) Console.WriteLine("Skipped SYSTEM.SAV Directory"); else if (dir.Contains("ProgramData")) Console.WriteLine("Skipped ProgramData Directory"); else if (dir.Contains("System Volume Information")) Console.WriteLine("Skipped System Volume Information Directory"); else if (dir.Contains("$SysReset")) Console.WriteLine("Skipped $SysReset Directory"); else GetAllDirs(dir); } } catch (UnauthorizedAccessException) { } }