判断命令是否在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

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

上一篇:react 图片懒加载


下一篇:项目产品化升级数据库脚本问题——如何查询两个数据库差异的库表或者数据