判断命令是否在PATH环境变量中

时间:2021-07-02 15:52:09   收藏:0   阅读:0
 1 public static bool IsInPath(string command)
 2 {
 3     bool isInPath = false;
 4     // 判断PATH中是否存在 命令
 5     foreach (string test in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(;))
 6     {
 7         string path = test.Trim();
 8         if (!String.IsNullOrEmpty(path) && File.Exists(path = Path.Combine(path, command)))
 9         {
10             isInPath = true;
11             break; // 如果在PATH中找到 ,则退出循环
12         }
13     }
14 
15     return isInPath;
16 }
IsInPath("cmd.exe") // 返回 bool值

 

参考:http://csharptest.net/526/how-to-search-the-environments-path-for-an-exe-or-dll/index.html

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!