Marcus
Marcus

Reputation: 6717

Find a file by file name

What is the best way to find a file by its file name and then return its path?

e.g.

public string GetFilePath(string filename)
{
    // some work to get the path
    return filepath;
}

I have tried this but unsuccessfully

public string GetFileContent(string filename)
{
    DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(@"c:\");
    FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles("*" + filename + "*.*");

    foreach (FileInfo foundFile in filesInDir)
    {
        string fullName = foundFile.FullName;
        return fullName;
    }
    return "found nothing";
}

Is there a best practice approach for finding a file by its file name on the hard drive?

Upvotes: 0

Views: 1706

Answers (4)

Janty
Janty

Reputation: 1714

Please try this.

string[] files = Directory.GetFiles(dir);
foreach(string file in files)
{
    if(Path.GetFileName(file).Contains("Your filename"))
    {
      do stuffs...
    }
}

For Performance:

IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*",System.IO.SearchOption.AllDirectories);

string searchTerm = @"Visual Studio";

// Search the contents of each file. 
// A regular expression created with the RegEx class 
// could be used instead of the Contains method. 
// queryMatchingFiles is an IEnumerable<string>. 

var queryMatchingFiles =
from file in fileList
let fileText =Path.GetFileName(file)
where fileText.Contains(searchTerm)
select file.FullName;

Upvotes: 1

Oliver
Oliver

Reputation: 9002

You can use Directory.GetFiles to recursively search a directory for matching files:

The working example below will find all of the full paths for files matching "Hello" in directory "C:\".

    static void Main(string[] args)
    {
        var files = GetFilePaths("*Hello*", "C:\\");

        foreach (var file in files)
        {
            Console.WriteLine(file);
        }
    }

    public static IEnumerable<string> GetFilePaths(string pattern, string directory)
    {
        return Directory.GetFiles(directory, pattern, SearchOption.AllDirectories);
    }

EDIT

The following answer is better as it get's around access denied issues:

Ignore folders/files when Directory.GetFiles() is denied access

Upvotes: 0

Dustin Kingen
Dustin Kingen

Reputation: 21245

If I was looking for a file and I did not know the location then I would use the bultin Windows Search.

See the code samples page for examples specifically the DSearch code sample.

The DSearch code sample demonstrates how to create a class for a static console application to query Windows Search using the Microsoft.Search.Interop assembly for ISearchQueryHelper.

Otherwise the Directory class has builtin helpers such as Directory.GetFiles which will do pattern matching.

public static string[] GetFiles(
    string path,
    string searchPattern,
    SearchOption searchOption
)

Upvotes: 0

decPL
decPL

Reputation: 5402

Try this:

Directory.GetFiles(@"c:\", filename, SearchOption.AllDirectories).FirstOrDefault()

Upvotes: 2

Related Questions