开发者

How to retrieve installed app version from registry?

I want to get the version numbers of the softwares installed in my machine...is it possible using Registry class in C#?

     RegistryKey regKey;

    public void ReadReg()
    {
        regKey = Registry.LocalMachine;
       开发者_JAVA技巧 RegistryKey sk= regKey.OpenSubKey("SOFTWARE\\");
        string[] subKeys = sk.GetSubKeyNames();

        foreach (string  sub in subKeys)
        {
            Console.WriteLine(sub);
        }            
    }


There is no standard and generally accessible location where installers store product information. However the installer database does contain this information (where products use MSI—directly or indirectly) and this information is available via WMI.

WMI is also usable from .NET with System.Management types. The query:

select name,version from Win32_Product

Adapting the sample here to this query shouldn't be too hard.


And what key are you going to look at, and how do you determine it's a version number? There are no guarantees as it will change from vendor to vendor.

An alternative would be to enumerate executables on your machine (say in C:\Program Files\ and it's subfolders), and use FileVersionInfo to extract the version information from the executable itself:

FileVersionInfo fi = FileVersionInfo.GetVersionInfo("yourexe.exe");


There is no guarantee you'll find version information about a program in the registry. I do use the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\programname to find the full path but it still depends on the setup/install program to set this value. If it was nice enough to use this convention you can use the full path to call GetFileVersionInfo(...).

It's still up to the developer to fill in the useful resource information like version, company name, description, copyright, etc... You can tell some programs have the old default "To Do" placeholders in the resource. Others, even Microsoft, will forget to include the company name in the resource for programs like Dumprep. Other Exe's created without Visual Studio don't bother to include any resource segment that can be accessed by GetFileVersionInfo().

Ultimately, I've found this function to be the most reliable. Also note, before calling GetFileVersionInfo() make sure you call GetFileVersionSize(). If it's a x64 version of Windows, in some cases you may need to call Wow64DisableWow64FsRedirection(...) & Wow64RevertWow64FsRedirection(...) or use SysNative if the program you're looking at is in a system folder that is redirected.


You can get version as -

string version = sk.GetValue("Version");

If Version key is available it will return value, else it will return null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜