Search code examples
c#visual-studioversion

Programmatically retrieve version of an installed application


I want to programmatically retrieve the version of an installed application (which is currently running), of which I have the name of the running process. If possible, retrieving the install directory would also be appreciated, but that is optional.

I've searched at a lot of places, and some questions looked similar, but they do not give me what I ask for.

To be a bit more specific, right now I want to do this for Visual Studio i.e. I have a WPF app, which is running alongside Visual Studio & given that I know the process name for Visual Studio i.e. "devenv", how can I get the version information of Visual Studio installed on my machine, from the WPF app? This is just an example, don't assume anything particular to Visual Studio. In the general case, we'd have an app running, for which we know the Process name & want its installed version.

Can you please provide the C# code for doing this?


Solution

  • This is gonna be simple. All kind of system related information will be present in Registry. (i.e) If you open regedit, you may find various HKEY. Now, please navigate to the following location.

    " HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall "

    You can find many folders inside this location, in which the name of the folder will be encrypted. Those folders indicates the installed application in the current machine.

    In each folder there will be many key and data pair of values. In that you can find DisplayName and DisplayVersion. So this DisplayVersion gives you the actual version of your application.

    So, How to achieve this through code?

     RegistryKey rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
    
                List<string> insApplication = new List<string>();
    
                if (rKey != null && rKey.SubKeyCount > 0)
                {
                    insApplication = rKey.GetSubKeyNames().ToList();
                }
    
                int i = 0;
    
                string version = "";
    
                foreach (string appName in insApplication)
                {
    
                    RegistryKey finalKey = rKey.OpenSubKey(insApplication[i]);
    
                    string installedApp = finalKey.GetValue("DisplayName").ToString();
    
                    if (installedApp == "Google Chrome")
                    {
                        version = finalKey.GetValue("DisplayVersion").ToString();
                        return;
                    }
                    i++;
                }