C# Search file in computer

12,206

Solution 1

Move your try catch into the foreach (string d in Directory.GetDirectories(sDir)) loop so it continues to process on error.

Solution 2

The problem is you are hitting a folder you do not have read privileges to. You will need to either run your program as administrator or will need to have it skip over files and folders you do not have read rights to. Using Directory.EnumerateFiles will let you simplify your code and it should not try to open folders it does not have read rights to.

foreach (string s in Directory.GetLogicalDrives())
{
    if (list.Count == 0)
    {
        foreach (string f in Directory.EnumerateFiles(s, file, SerchOption.AllDirectories))
        {
            try
            {
                textBox2.Text = f;
                list.Add(f);
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }

        }
    }
}

Solution 3

I know it's an old thread, but it ranked quite high in Google, so I thought I would clarify a free things for others that stumble across it..

First of all: the programme cannot access C:\Documents and Settings because it doesn't exist on a windows 7. It's a symbolic link without any actual content except a pointer. But the programme doesn't know that since it sees it as a regular directory. That is why it fails...

I would wrap the whole thing in a try, catch (without any action in the catch part)..

Hope it helps someone from trying any of the proposed answers..

Share:
12,206
Ionut Ungureanu
Author by

Ionut Ungureanu

Updated on June 04, 2022

Comments

  • Ionut Ungureanu
    Ionut Ungureanu about 2 years

    I am currently developing a C# application to search a file in the computer. The GUI has two text fields: one for input(the name for the file to be searched) and one for displaying the path to the file (if it's found).

    Now, the problem is that my application is skipping my logical C: drive. Here is some piece of code:

    foreach (string s in Directory.GetLogicalDrives() )
    {
        if (list.Count == 0)
        {
            foreach (string f in Directory.GetFiles(s, file))
            {
                textBox2.Text = f;
                list.Add(f);
            }
            if (list.Count == 0)
            {
                search(s);
            }
        }
    }
    

    And the search method:

    private void search(string sDir)
    {
        try
        {
            foreach (string d in Directory.GetDirectories(sDir))
            {
                if (list.Count == 0)
                {
                    Console.WriteLine(d);
                    foreach (string f in Directory.GetFiles(d, file))
                    {
                        textBox2.Text = f;
                        list.Add(f);
                    }
                    if (list.Count == 0)
                    {
                        search(d);
                    }
                }
            }
        }
        catch (System.Exception excpt)
        {
            Console.WriteLine(excpt.Message);
        }
    }
    

    Here is some output:

    C:\$Recycle.Bin
    C:\$Recycle.Bin\S-1-5-21-1794489696-3002174507-1726058468-1000
    C:\Config.Msi
    C:\de cautat
    C:\de cautat\database
    C:\de cautat\fut_bot
    C:\de cautat\helper
    C:\de cautat\images
    C:\de cautat\itemiinfo
    C:\de cautat\JSONs
    C:\de cautat\JSONs\PLAYER
    C:\de cautat\JSONs\USER
    C:\de cautat\requests
    C:\Documents and Settings
    A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
    Access to the path 'C:\Documents and Settings' is denied.
    D:\$RECYCLE.BIN
    D:\$RECYCLE.BIN\S-1-5-21-1794489696-3002174507-1726058468-1000
    D:\$RECYCLE.BIN\S-1-5-21-2637989945-465084399-3498881169-1000
    etc
    

    Can anyone help me and tell what is the problem when accesing those folders? Thanks!

    Note: I am using Microsoft Visual Studio 2010 on Windows 7 x64 (.NET Framework 2.0).

  • Ionut Ungureanu
    Ionut Ungureanu over 12 years
    I have built a release version of the app and run it as administrator. Still the same problem. :(
  • Ionut Ungureanu
    Ionut Ungureanu over 12 years
    Thanks! That was the problem.
  • Scott Chamberlain
    Scott Chamberlain over 12 years
    @IonutUngureanu you should really just use EnumerateFiles instead of GetFiles, don't re-invent the wheel if you don't have to (also my example posted includes M.Babcocks fix.)
  • M.Babcock
    M.Babcock over 12 years
    @ScottChamberlain - I've done this both ways and have ended up with more complications because EnumerateFiles doesn't report status and if done on the root directory as you suggest, it will hang the application. GetFiles allows finer grain control over the search.
  • Scott Chamberlain
    Scott Chamberlain over 12 years
    @M.Babcock Neither "Report status", I don't understand what you mean by that. Also GetFiles will lock up your machine, I am referring to EnermateFiles which is lazy loaded, it only takes CPU cycles to find the next entry, it does not do the work up front.
  • Scott Chamberlain
    Scott Chamberlain over 12 years
    Did you mean "Report status" mean you can get the file count in the current query?
  • M.Babcock
    M.Babcock over 12 years
    I must be mistaken, and have used GetFiles with AllDirectories rather than EnumerateFiles. How does EnumerateFiles handle directories that the user doesn't have permission to? Your example below is not quite the same as my suggestion. Having the try catch within the foreach now is practically useless since you'll only capture errors while setting the textbox value and adding the entry to the list.
  • Scott Chamberlain
    Scott Chamberlain over 12 years
    It will return a entry for every file that matches the search pattern and the folder the file resides in has "List" permissions. If the folder does not have list permissions it will just skip the folder (as there is nothing to return in the IEnuemerable)