简体   繁体   中英

Find a file by file name

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

eg

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?

尝试这个:

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

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;

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
)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM