如何在系统路径中找到文件的位置,例如 get-command 或 where?

How can I find the location of a file in the system path, like get-command or where?

我想查找系统路径下是否有某个DLL。在 Powershell 中,我可以使用 get-command myfile.dll,在 cmd 中我可以使用 where myfile.dll.

我如何在 C# 中做到这一点?我必须手动解析 Environment.GetEnvironmentVariable("path"),还是有更好的方法?

不确定 where,但 Get-Command 使用以下算法(过于简化)在内部解析文件位置:

  1. 解析Environment.GetEnvironmentVariable("PATH")
  2. Directory.EnumerateFiles()
  3. 枚举每个 PATH 条目中的所有文件
  4. 将输入文件名与目录中的每个文件名进行 case-insensitive 字符串比较