Search code examples
c#windowsfilepath

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?


Solution

  • 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