【发布时间】:2019-02-20 15:41:19
【问题描述】:
我有下面的代码。它工作正常,但我想知道是否有一种不那么冗长的方法。是否有一个库方法来查看是否存在一个识别 PATH 环境变量中路径的文件,这样我就可以执行if (FileExistsInAnyPath("robocopy.exe") 之类的操作,而无需从PATH 中提取所有路径
string foundIt = "";
string[] paths = (Environment.GetEnvironmentVariable("Path")).Split(';');
foreach (string path in paths)
{
if (File.Exists((path + "\\robocopy.exe")))
{
foundIt = (path + "\\robocopy.exe");
break;
}
}
if (!string.IsNullOrEmpty(foundIt))
{
// do something with fq path name
Console.WriteLine("found it here: " + foundIt);
}
【问题讨论】:
标签: c# path file-exists