为了在 PC 上快速查找文件,我长期以来一直在使用该库 - https://github.com/VladPVS/FastSearchLibrary
public static string _keywords = "TestFile, .rar, .zip, .mp3, Bloody6, Artificial";
public void TestSe()
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
List<string> keywords = _keywords.Split(',').ToList(); // #2 <--------
//List<string> keywords = new List<string>() {
// @"TestFile",
// @".rar",
// @".zip",
// @".mp3",
// @"Bloody6",
// @"Artificial" }; // #1 <----------
List<string> folders = new List<string>();
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady)
{
string driveRoot = drive.RootDirectory.FullName;
folders.Add(driveRoot);
}
}
searcher = new FileSearcherMultiple(folders, (f) =>
{
foreach (var keyword in keywords)
if (f.Name.Contains(keyword))
return true;
return false;
}, tokenSource);
List<FileInfo> files = new List<FileInfo>();
searcher.FilesFound += (sndr, arg) =>
{
lock (locker)
{
arg.Files.ForEach((f) =>
{
files.Add(f);
new Thread(() =>
{
//my work
}).Start();
});
}
};
searcher.SearchCompleted += (sndr, arg) =>
{
//ended
};
searcher.StartSearchAsync();
}
我决定在全局字符串中显示关键字列表(根据需要)。但由于某种原因,搜索开始变得迟钝。 如果你像#1那样直接使用这个列表,那么它会根据3000+条关键字找到所有文件。 如果您使用#2,它会通过关键字“Bloody6”、“Artifical”找到 4-5 个文件。可能是什么问题?
最佳答案
很可能是因为如果您使用 _keywords.Split(',')
它将返回带空格的扩展名:
例如[space].rar
, [space].zip
, [space].mp3
等
您需要修剪这些值:
_keywords.Split(',').Select(s => s.TrimStart()).ToList();
关于c# - 要列出的字符串。它的工作方式不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54564565/