简体   繁体   中英

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

I want to find whether a certain DLL is available on the system path. in Powershell, I can use get-command myfile.dll , and in cmd I can use where myfile.dll .

How can I do that in C#? Do I have to manually parse Environment.GetEnvironmentVariable("path") , or is there a better way?

Not sure about where , but Get-Command internally resolves the file location using the following algorithm (overly simplified):

  1. Parse Environment.GetEnvironmentVariable("PATH")
  2. Enumerate all files in each individual PATH entry with Directory.EnumerateFiles()
  3. Perform a case-insensitive string comparison with the input file name against each file name in the directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM