how to find the execution path of a installed software

后端 未结 6 1349
夕颜
夕颜 2020-12-05 16:26

How can i find the execution path of a installed software in c# for eg media player ,vlc player . i just need to find their execution path . if i have a vlc player install

6条回答
  •  粉色の甜心
    2020-12-05 16:59

    This method works for any executable located in a folder which is defined in the windows PATH variable:

    private string LocateEXE(String filename)
    {
        String path = Environment.GetEnvironmentVariable("path");
        String[] folders = path.Split(';');
        foreach (String folder in folders)
        {
            if (File.Exists(folder + filename))
            {
                return folder + filename;
            } 
            else if (File.Exists(folder + "\\" + filename)) 
            {
                return folder + "\\" + filename;
            }
        }
    
        return String.Empty;
    }
    

    Then use it as follows:

    string pathToExe = LocateEXE("example.exe");
    

    Like Fredrik's method it only finds paths for some executables

提交回复
热议问题