简体   繁体   English

如何从注册表中检索已安装的应用程序版本?

[英]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#? 我想获取机器中安装的软件的版本号...可以在C#中使用注册表类吗?

     RegistryKey regKey;

    public void ReadReg()
    {
        regKey = Registry.LocalMachine;
        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. 但是,安装程序数据库确实包含此信息(在产品直接或间接使用MSI的情况下),并且可以通过WMI获得此信息。

WMI is also usable from .NET with System.Management types. WMI也可以从.NET与System.Management类型一起使用。 The query: 查询:

select name,version from Win32_Product

Adapting the sample here to this query shouldn't be too hard. 此处的示例适应此查询应该不会太困难。

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. 我确实使用注册表项HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\programname来查找完整路径,但它仍然取决于安装程序/安装程序来设置此值。 If it was nice enough to use this convention you can use the full path to call GetFileVersionInfo(...) . 如果使用该约定足够好,则可以使用完整路径来调用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. 其他公司,甚至包括Microsoft,都将忘记在Dumprep之类的程序的资源中包含公司名称。 Other Exe's created without Visual Studio don't bother to include any resource segment that can be accessed by GetFileVersionInfo() . 在没有Visual Studio的情况下创建的其他Exe不会费心地包含可由GetFileVersionInfo()访问的任何资源段。

Ultimately, I've found this function to be the most reliable. 最终,我发现此功能是最可靠的。 Also note, before calling GetFileVersionInfo() make sure you call GetFileVersionSize() . 另请注意,在调用GetFileVersionInfo()之前,请确保您调用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. 如果它是Windows的x64版本,则在某些情况下,您可能需要调用Wow64DisableWow64FsRedirection(...)Wow64RevertWow64FsRedirection(...) ,如果要查看的程序位于重定向的系统文件夹中,请使用SysNative。

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: 一种替代方法是枚举计算机上的可执行文件(例如在C:\\Program Files\\及其子文件夹中),然后使用FileVersionInfo从可执行文件本身中提取版本信息:

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

You can get version as - 您可以将版本获取为-

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

If Version key is available it will return value, else it will return null. 如果版本密钥可用,它将返回值,否则将返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM