[英]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 .如果我正在寻找一个文件并且我不知道该位置,那么我将使用 bultin Windows Search 。
See the code samples page for examples specifically the DSearch code sample.请参阅代码示例页面以获取特别是DSearch代码示例的示例。
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. DSearch 代码示例演示如何为静态控制台应用程序创建一个类,以使用 ISearchQueryHelper 的 Microsoft.Search.Interop 程序集查询 Windows Search。
Otherwise the Directory
class has builtin helpers such as Directory.GetFiles
which will do pattern matching.否则, Directory
类具有内置帮助器,例如Directory.GetFiles
将进行模式匹配。
public static string[] GetFiles(
string path,
string searchPattern,
SearchOption searchOption
)
You can use Directory.GetFiles
to recursively search a directory for matching files:您可以使用Directory.GetFiles
递归搜索目录以查找匹配文件:
The working example below will find all of the full paths for files matching "Hello"
in directory "C:\"
.下面的工作示例将在目录"C:\"
中找到与"Hello"
匹配的文件的所有完整路径。
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 当 Directory.GetFiles() 被拒绝访问时忽略文件夹/文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.