问题描述
我试图搜索特定的文件名上所有卷的系统上,除了系统卷信息或Windows目录并打印出每个文件的每一个完整路径。
I'm trying to search for specific file name on all volumes on the system, except System Volume Information or Windows Directories and print out every full path for each file.
我试图用 Directory.GetFiles
,但它只能搜索器内部驱动器C:\或驱动器D:\(取决于其指定的驱动器)
I've tried to use Directory.GetFiles
, but it only searche inside Drive C:\ or Drive D:\ (depends which drive is specified)
我如何能实现我的目标?
How can I achieve my goal?
例如我想运行的程序和名称搜索文件。 DS_Store的(用于使用MAC,现在我的文件系统已满这些文件的....)
For example I want to run program and search for file with name ".DS_Store" (used to use mac, and now my file system is full of these files....)
我真的很感激任何帮助。
预先感谢您
I would really appreciate any help.Thank you in advance
UPD。方案应具有管理员权限执行工作
推荐答案
作为cryptolocker分析的一部分,我写了一个原型,定位目标扩展,这里是如何使用它
As part of a cryptolocker analysis, I wrote a prototype that locates target extensions, here is how to use it
var cmd = new LocateTargetFilesCommand();
cmd.Execute()
这将搜索所有驱动器用于两个扩展:的.xlsx
和 .DOC
。最大的问题是处理权限问题。另外一个子文件夹可能有更广泛的权限,因此,如果您不能访问父文件夹,您仍然可以潜在地访问其根源。
It will search all the drives for two extensions: .xlsx
and .doc
. The biggest problem is to handle permissions issue. Also a child folder may have wider permissions, so if you cannot access a parent folder, you can still potentially access its roots.
最后, cmd.FoundTargets
将包含绝对路径找到的文件与所需扩展名
At the end, cmd.FoundTargets
will contain absolute paths to the found files with desired extensions
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
}
}
}
这篇关于搜索目录中的特定文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!