c#怎样根据文件名获取其所在的绝对路径
1个回答
展开全部
//fileName是我测试时候用的,仅供参考
private static void FindFilesNameFromDirectory(string fileName = "timezone")
{
List<string> findFilePath = new List<string>();
string[] drives = Environment.GetLogicalDrives();
foreach (string item in drives)
{
try
{
findFilePath.AddRange(searchFile(item, fileName));
}
catch
{
}
}
Console.WriteLine("{0}", string.Join("\r\n", findFilePath.ToArray()));
}
private static List<string> searchFile(string directory, string fileName)
{
List<string> findFilePath = new List<string>();
Regex reg = new Regex(fileName);
findFilePath.AddRange(Directory.GetFiles(directory).ToList().FindAll(x => reg.IsMatch(x)));
string[] childPath = Directory.GetDirectories(directory);
foreach (string path in childPath)
{
try
{
findFilePath.AddRange(searchFile(path, fileName));
}
catch
{
}
}
return findFilePath;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询