how to find the execution path of a installed software

后端 未结 6 1333
夕颜
夕颜 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:51

    Have a look at MsiEnumProductsEx

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-05 17:01

    None of the answers worked for me. After hours of searching online, I was able to successfully get the installation path. Here is the final code.

    public static string checkInstalled(string findByName)
        {
            string displayName;
            string InstallPath;
            string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
    
            //64 bits computer
            RegistryKey key64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            RegistryKey key = key64.OpenSubKey(registryKey);
    
            if (key != null)
            {
                foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
                {
                    displayName = subkey.GetValue("DisplayName") as string;
                    if (displayName != null && displayName.Contains(findByName))
                    {
    
                        InstallPath = subkey.GetValue("InstallLocation").ToString();
    
                        return InstallPath; //or displayName
    
                    }
                }
                key.Close();
            }
    
            return null;
        }
    

    you can call this method like this

    string JavaPath = Software.checkInstalled("Java(TM) SE Development Kit");
    

    and boom. Cheers

    0 讨论(0)
  • 2020-12-05 17:04

    I used the CurrentVersion\Installer\Folders registry key. Just pass in the product name.

    private string GetAppPath(string productName)
        {
            const string foldersPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\Folders";
            var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    
            var subKey = baseKey.OpenSubKey(foldersPath);
            if (subKey == null)
            {
                baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
                subKey = baseKey.OpenSubKey(foldersPath);
            }
            return subKey != null ? subKey.GetValueNames().FirstOrDefault(kv => kv.Contains(productName)) : "ERROR";          
        }
    
    0 讨论(0)
  • 2020-12-05 17:05

    This stackoverflow.com article describes how to get the application associated with a particular file extension.

    Perhaps you could use this technique to get the application associated with certain extensions, such as avi or wmv - either Medial Player or in your case VLC player?

    0 讨论(0)
  • 2020-12-05 17:07

    Using C# code you can find the path for some excutables this way:

    private const string keyBase = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths";
    private string GetPathForExe(string fileName)
    {
        RegistryKey localMachine = Registry.LocalMachine;
        RegistryKey fileKey = localMachine.OpenSubKey(string.Format(@"{0}\{1}", keyBase, fileName));
        object result = null;
        if (fileKey != null)
        {
            result = fileKey.GetValue(string.Empty);
            fileKey.Close();
        }
    
    
        return (string)result;
    }
    

    Use it like so:

    string pathToExe = GetPathForExe("wmplayer.exe");
    

    However, it may very well be that the application that you want does not have an App Paths key.

    0 讨论(0)
提交回复
热议问题