如何在程序文件夹c#中查找特定文件的文件路径 [英] how to find a file path of a particular file in program folder c#
问题描述
我需要找到特定文件的路径。我知道文件的文件名和扩展名。但我不知道文件所在的位置。该文件将位于 C:\Program Files 的任何子目录中。所以我需要在程序文件夹中搜索文件。我的文件名是 CASdb.accdb
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
推荐答案
只是探索
Just explore
string[] filePaths = System.IO. Directory.GetFiles("C:\Program Files","CASdb.accdb",SearchOption.AllDirectories) ;
使用'EnumerateFiles的解决方案,可能比使用'GetFiles更快:
A solution using 'EnumerateFiles, which may be faster than using 'GetFiles:
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;
调用此方法的示例:
// note that 'result is passed by reference
string result;
if (findFilePath(@"C:\Program Files", @"CASdb.accdb", out result))
{
// result contains path to file
}
讨论:
首先,请注意有一些目录在引导驱动程序'Program Files文件夹,如'WindowsApps',除非您拥有TrustedInstaller的访问权限,否则无法使用'GetFiles或'EnumerateFiles访问它。例如,使用Win 8,请参阅:[ ^ ]。尝试访问具有TrustedInstaller状态的目录将导致应用程序中断错误。
如何在FileExplorer中创建TrustedInstaller访问(手动):[ ^ ]。
获取C#代码中的这种权限很复杂:从这里开始定向:[ ^ ],然后看:[ ^ ],[ ^ ]。
修改TrustedInstaller状态目录访问可能是出于安全原因你不应该做的事情 ......除非你是一个合法的SysAdmin。如果你需要接受这项研究,请准备好进行一些认真的学习!
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 !
这是另一种在找到文件时停止搜索的方法。
This is another approach that will stop searching when the file is found.
string foundFilePath = FindFile(@"C:\Program Files", "CASdb.accdb");
请注意,此方法是递归的
Note that this method is recursive
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.
这篇关于如何在程序文件夹c#中查找特定文件的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!