Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I have a requirement to find the path of a particular file. I know the file name and extension of the file. but i don't know where the file resides. The file will in any of the sub directories of C:\Program Files. so i need to search inside the program folder for the file. My file name is CASdb.accdb
Posted
Comments
BillWoodruff 25-Oct-14 2:00am    
Can you assume that the file will always be in a directory named 'CAS ?

Just explore
string[] filePaths = System.IO. Directory.GetFiles("C:\Program Files","CASdb.accdb",SearchOption.AllDirectories) ;
 
Share this answer
 
v2
Comments
jinesh sam 25-Oct-14 1:46am    
thanks i get the result "C:\\Program Files\\CAS\\CASdb.accdb"
but it taking to much time. is there any other method or atleast to stop the search when the required file is found
Sergey Alexandrovich Kryukov 25-Oct-14 1:50am    
The search itself make little sense. You need to devise something better. This search is fast enough; there is no such thing as miracle. I suggest you accept the answer formally (green "Accept" button).
—SA
Sergey Alexandrovich Kryukov 25-Oct-14 1:50am    
My 5.
—SA
BillWoodruff 25-Oct-14 2:02am    
Note that 'GetFiles will fail if it tries to parse a Directory it does not have access rights to.
A solution using 'EnumerateFiles, which may be faster than using 'GetFiles:
C#
private bool findFilePath(string directoryPath, string fileName, out string result)
{
    string fullPath = Path.Combine(directoryPath, fileName);

    if (File.Exists(fullPath))
    {
        result = fullPath;
        return true;
    }

    // note this initialization required here
    // since 'result has been passed by reference using 'out
    result = string.Empty;

    try
    {
        result = Directory.EnumerateFiles(directoryPath, fileName, SearchOption.AllDirectories).First();
    }
    catch
    {
        // something went wrong ...
        return false;
    }

    // valid result
    return true;
Example of calling this method:
C#
// note that 'result is passed by reference
string result;

if (findFilePath(@"C:\Program Files", @"CASdb.accdb", out result))
{
    // result contains path to file
}
Discussion:

First, note that there are some Directories in the boot-drive 'Program Files folder, like 'WindowsApps, that you cannot access with either 'GetFiles or 'EnumerateFiles unless you have the access right of "TrustedInstaller." For example with Win 8 see:[^]. Trying to access directories with TrustedInstaller status will result in an application breaking error.

For how to create "TrustedInstaller" access (manually) in the FileExplorer: [^].

Getting this kind of permission in C# code is complex: start here to get oriented: [^], and then see: [^], [^].

Modifying TrustedInstaller status directory access is probably something you should not be doing for security reasons ... unless you are a legitimate SysAdmin. Get ready for some serious study if you need to take this on !
 
Share this answer
 
v4
This is another approach that will stop searching when the file is found.
C#
string foundFilePath = FindFile(@"C:\Program Files", "CASdb.accdb");


Note that this method is recursive
C#
private string FindFile(string directory, string fileName)
{
    string foundFileName = null;
    try
    {
        foundFileName = Directory.GetFiles(directory, fileName).FirstOrDefault();
        if (String.IsNullOrEmpty(foundFileName))
        {
            foreach (string dir in Directory.GetDirectories(directory))
            {
                foundFileName = FindFile(dir, fileName);
                if (!String.IsNullOrEmpty(foundFileName))
                    break;
            }
        }
    }
    catch { } // The most likely exception is UnauthorizedAccessException
              // and there is not much to do about that

    return foundFileName;
}


I have not done any bench marking to see if this way is faster or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900