0

I can't figure it out yet.

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;
                }
            }
        }

In this case i have in textToSearch 6 items:

form1,form2,hi,world,44,44

I found that if the first item in textToSearch is Form1 with large F it will find it but if it's form1 with small f it will not. I tried to add ToLower() like this: if (File.ReadAllText(file).Contains(textToSearch[i].ToLower())) but it didn't help/change much. How can i make that it will consider the same result as form1 or Form1 or FoRM1 ?

  • 1
    Here is how to perform the case insensitive search: http://stackoverflow.com/questions/444798/case-insensitive-containsstring. As a side note - read each file only once. Now you are reading each file multiple times. – Pawel Jul 03 '16 at 15:26

1 Answers1

2

Instead of using Contains, you might want to use IndexOf, which makes case-insensitive comparisons easier with the StringComparison enumerated values.

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);
        }
    }
}

Using the original commented out line with Contains the result is:

I have form2 and hello world contains form2
I have form2 and hello world contains world

Using IndexOf, the result is:

I have Form1 contains form1
I have form2 and hello world contains form2
I have form2 and hello world contains world
BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146