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

 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

posted @ 2021-07-01 23:08  WIN&迷失  阅读(170)  评论(0编辑  收藏  举报