首页 > 其他 > 详细

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

时间:2021-07-02 00:27:43      阅读:27      评论: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

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

原文:https://www.cnblogs.com/jonah/p/14961357.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!