-2

I'm trying to search files in a folder that has a lot of folders, with a name that contain specific string.

I'm able to do it but it's taking me about 2 minutes and this is too much time for me.

This is the function:

private void Search()
{
    foreach (var file in Directory.EnumerateFiles(@"P:\system\mail\", "*" + textBox1.Text + "*.pdf*", SearchOption.AllDirectories))
    {
        this.Invoke(new MethodInvoker(delegate ()
        {
            listBoxControl1.Items.Add(file);
        }));

        if (XtraMessageBox.Show("Open the file: " + file  + " ?", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
        {
            Process.Start(file);
        }
    }
}

And this is where I'm using the function:

private async void simpleButton1_Click(object sender, EventArgs e)
{
    labelControl1.Text = "Status: Please wait . . .";
    Stopwatch watch = new Stopwatch();
    watch.Start();
    await Task.Run(() => Search());
    watch.Stop();
    labelControl1.Text =  "The process done in " + watch.Elapsed.TotalMinutes.ToString() + " minutes.";
}

The goal is to do it like the search in windows that takes me 4-7 seconds.

SysDragon
  • 9,692
  • 15
  • 60
  • 89
Ari Perez
  • 7
  • 5
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/215289/discussion-on-question-by---fastest-way-to-search-specific-file-in-subdire). – Samuel Liew Jun 04 '20 at 09:15

2 Answers2

4

To compare your search to Windows own search functionality is somewhat invalid because Windows search takes advantage of indexing the filesystem, while in your current implementation, you do not.

But there's good news: You can do it, too.

There are several ways to achieve similar response times, some are faster, some are more precise.

For example you could:

  1. Perform searches in fixed intervals and use the results. Drawback: List may be outdated. OR...
  2. Have an initial search on App start, then use FileSystemWatcher to get notified about FileSystem events (File new, File deleted, File moved ...) to update your internal index. Use that index as your source of information. Drawback: FSW can be a pain to deal with.
  3. Find a way to take advantage of windows own indexes. See:

I don't know if 3. is working for you. 1. may be out of the race because you probably don't want potentially outdated data. So I'd go with 2, but give 3. a shot, too.

Fildor
  • 14,510
  • 4
  • 35
  • 67
2

Try to use Directory.GetFiles:

string[] files = Directory.GetFiles(filePath, "*.pdf", SearchOption.AllDirectories);
SysDragon
  • 9,692
  • 15
  • 60
  • 89
  • Not good for me friend, im already tried it. and if you give an answer please try to stick to my function. thanks. – Ari Perez Jun 04 '20 at 06:39
  • 1
    Why would `GetFiles` be faster than `EnumerateFiles` – mjwills Jun 04 '20 at 06:49
  • 1
    _"The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, **when you are working with many files and directories, EnumerateFiles can be more efficient**."_ - [Docs](https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netcore-3.1#System_IO_Directory_GetFiles_System_String_System_String_System_IO_SearchOption_) – Fildor Jun 04 '20 at 06:54