まだわかりません。
void DirSearch(string rootDirectory, string filesExtension, string[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
{
List<string> resultsoftextfound = new List<string>();
List<string> filePathList = new List<string>();
List<string> restrictedFiles = new List<string>();
int numberoffiles = 0;
int numberofdirs = 0;
int countTextToSearch = 0;
try
{
filePathList = SearchAccessibleFilesNoDistinct(rootDirectory, null).ToList();
}
catch (Exception err)
{
string ad = err.ToString();
}
foreach (string file in filePathList)
{
try
{
_busy.WaitOne();
if (worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
for (int i = 0; i < textToSearch.Length; i++)
{
List<MyProgress> prog = new List<MyProgress>();
if (File.ReadAllText(file).Contains(textToSearch[i].ToLower()))
{
resultsoftextfound.Add(file + " " + textToSearch[i]);
numberoffiles++;
prog.Add(new MyProgress { Report1 = file, Report2 = numberoffiles.ToString() });
backgroundWorker1.ReportProgress(0, prog);
}
}
numberofdirs++;
label1.Invoke((MethodInvoker)delegate
{
label1.Text = numberofdirs.ToString();
label1.Visible = true;
});
}
catch (Exception)
{
restrictedFiles.Add(file);
continue;
}
}
}
この場合、私はtextToSearchに6つのアイテムを持っています:
form1、form2、hi、world、44,44
textToSearchの最初の項目がFが大きいForm1の場合は検出されますが、fが小さいform1の場合は検出されないことがわかりました。ToLower()を次のように追加しようとしました:if(File.ReadAllText(file).Contains(textToSearch [i] .ToLower()))が、あまり役に立ちませんでした。form1またはForm1またはFoRM1と同じ結果を考慮するようにするにはどうすればよいですか?
を使用する代わりに、を使用Contains
することをお勧めしますIndexOf
。これにより、StringComparison
列挙値との大文字と小文字を区別しない比較が容易になります。
string[] testData = { "No results to see here", "I have Form1", "I have form2 and hello world" };
string[] textToSearch = { "form1", "form2", "hi", "world", "44", "44" };
foreach (string data in testData)
{
foreach (string seek in textToSearch)
{
//if (data.Contains(seek))
if (data.IndexOf(seek, StringComparison.InvariantCultureIgnoreCase)>=0)
{
Console.WriteLine("{0} contains {1}", data, seek);
}
}
}
元のコメント化された行をContains
結果とともに使用すると、次のようになります。
I have form2 and hello world contains form2
I have form2 and hello world contains world
を使用するIndexOf
と、結果は次のようになります。
I have Form1 contains form1
I have form2 and hello world contains form2
I have form2 and hello world contains world
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加