简体   繁体   English

使用 c# 获取应用程序的安装版本

[英]get installed version of an application using c#

I would to get installed version of an application (say, MyApp) using C#.我会使用 C# 获得应用程序的安装版本(例如,MyApp)。 I will do this much, 1. Create a 'Set Up' for MyApp of version 5.6 2. Install MyApp.我会做这么多,1. 为 5.6 版的 MyApp 创建一个“设置” 2. 安装 MyApp。

I will create another application (say VersionTracker)to get the version of installed applications.我将创建另一个应用程序(比如 VersionTracker)来获取已安装应用程序的版本。 So if I pass the name 'MyApp' I would like to get the version as '5.6'.因此,如果我传递名称“MyApp”,我希望将版本设为“5.6”。 If another application say Adobe Reader is installed in my system, I want to get the version of Adobe Reader if I pass 'Adobe Reader'.如果另一个应用程序说我的系统中安装了 Adobe Reader,如果我通过“Adobe Reader”,我想获得 Adobe Reader 的版本。

I need to know how to build 'VersionTracker'我需要知道如何构建“VersionTracker”

The first and the most important thing is that not all applications do save their version somewhere in the system.首先也是最重要的一点是,并非所有应用程序都将其版本保存在系统的某个位置。 To be honest, only a few of them do that.老实说,只有少数人这样做。 The place where you should look are the Windows Registry.您应该查看的地方是 Windows 注册表。 Most of installed applications put their installation data into the following place:大多数已安装的应用程序将其安装数据放入以下位置:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

However, it's not that easy - on 64bit Windows, the 32bit (x86) applications save their installation data into another key, which is:然而,这并不容易——在 64 位 Windows 上,32 位 (x86) 应用程序将其安装数据保存到另一个密钥中,即:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

In these keys there are many keys, some of them have got "easy-readable" name, such as Google Chrome , some of them got names such as {63E5CDBF-8214-4F03-84F8-CD3CE48639AD} .在这些键中有很多键,其中一些具有“易于阅读”的名称,例如Google Chrome ,其中一些具有诸如{63E5CDBF-8214-4F03-84F8-CD3CE48639AD}名称。 You must parse all these keys into your application and start looking for the application names.您必须将所有这些键解析到您的应用程序中并开始查找应用程序名称。 There are usually in DisplayName value, but it's not always true. DisplayName中通常有值,但并不总是如此。 The version of the application is usually in DisplayVersion value, but some installers do use another values, such as Inno Setup: Setup Version , ... Some application do have their version written in their name, so it's possible that the application version is already in the DisplayName value.应用程序的版本通常在DisplayVersion值中,但一些安装程序确实使用其他值,例如Inno Setup: Setup Version ,... 某些应用程序的版本确实写在他们的名称中,因此应用程序版本可能已经是在DisplayName值中。

Note: It's not easy to parse all these registry keys and values and to "pick" the correct values.注意:解析所有这些注册表项和值并“挑选”正确的值并不容易。 Not all installers save the application data into these keys, some of them do not save the application version there, etcetera.并非所有安装程序都将应用程序数据保存到这些密钥中,其中一些安装程序不会将应用程序版本保存在那里,等等。 However, it's usual that the application use these registry keys.但是,应用程序通常使用这些注册表项。 [Source: StackOverflow: Detecting installed programs via registry , browsing my own registry] [来源: StackOverflow:通过注册表检测安装的程序,浏览我自己的注册表]

Alright, so now when you know where you should look, you have to program it all in C#.好的,所以现在当您知道应该看哪里时,您必须在 C# 中对其进行编程。 I won't write the application for you, but I'll tell you what classes you should use and how to.我不会为您编写应用程序,但我会告诉您应该使用哪些类以及如何使用。 First, you need these:首先,你需要这些:

using System;
using Microsoft.Win32;      

To get to your HKEY_LOCAL_MACHINE , create a RegistryKey like this:要访问您的HKEY_LOCAL_MACHINE ,请创建一个这样的RegistryKey

RegistryKey baseRegistryKey = Registry.LocalMachine;

Now you need to define subkeys:现在您需要定义子键:

string subKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
// or "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"

Now you need to go to the subkey, so create a new RegistryKey :现在你需要 go 到子键,所以创建一个新的RegistryKey

RegistryKey uninstallKey = baseRegistryKey.OpenSubKey(subKey);

Now you need to go thru all the subkeys that are there, so first we get the names of all the subkeys:现在你需要 go 通过所有的子键,所以首先我们得到所有子键的名称:

string[] allApplications = uninstallKey.GetSubKeyNames();

Now you must go thru all the subkeys yourself , one by one, by creating a new registry key (you don't have to, but I'll do it):现在,您必须自己通过所有子项 go一个一个地创建一个新的注册表项(您不必这样做,但我会这样做):

RegistryKey appKey = baseRegistryKey.OpenSubKey(subKey + "\\" + applicationSubKeyName);

where applicationSubKeyName is the name of the subkey you're currently checking.其中applicationSubKeyName是您当前正在检查的子项的名称。 I recommend foreach statement, which helps you (you must however have some experience with C# already, I'm not going to tell you how to use foreach here).我推荐foreach语句,它可以帮助您(但是您必须已经对 C# 有一定的经验,我不会在这里告诉您如何使用foreach )。

Now check the application's name and compare it with name of your desired application (you cannot rely on the subkey name, because, as I already said, they can be called for example {63E5CDBF-8214-4F03-84F8-CD3CE48639AD} , so you must check the name here):现在检查应用程序的名称并将其与所需应用程序的名称进行比较(您不能依赖子项名称,因为正如我已经说过的,它们可以被称为例如{63E5CDBF-8214-4F03-84F8-CD3CE48639AD} ,所以你必须在此处检查名称):

string appName = (string)appKey.GetValue("DisplayName");

If it's the correct application (you must check it yourself), find the version:如果它是正确的应用程序(你必须自己检查),找到版本:

string appVersion = (string)appKey.GetValue("DisplayVersion");

Et voilà, you have the version.等等,你有版本。 At least there's like a 60 - 80% chance you have...至少有 60% 到 80% 的机会...

Remember!记住! If some key or value doesn't exist, the method returns null .如果某些键或值不存在,则该方法返回null Remember to check if the returned value is null everytime, otherwise your application will crash.记得每次检查返回值是否为null,否则你的应用程序会崩溃。

Where to find more?在哪里可以找到更多? The Code Project: Read, write and delete from registry with C#代码项目:使用 C# 从注册表中读取、写入和删除

I really hope I helped you.我真的希望我能帮助到你。 And if you wanted to know something else and I didn't understand your question, then, please, ask better next time.如果你想知道别的东西而我不明白你的问题,那么,请下次问得更好。 :) :)

    ///
/// Author : Muhammed Rauf K
/// Date : 03/07/2011
/// A Simple console application to create and display registry sub keys
///

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// it's required for reading/writing into the registry:
using Microsoft.Win32;


namespace InstallationInfoConsole
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("Registry Information ver 1.0");
Console.WriteLine("----------------------------");

Console.Write("Input application name to get the version info. (for example 'Nokia PC Suite'): ");
string nameToSearch = Console.ReadLine();

GetVersion(nameToSearch);

Console.WriteLine("----------------------------");

Console.ReadKey();


}

///
/// Author : Muhammed Rauf K
/// Date : 03/07/2011
/// Create registry items
///
static void Create()
{
try
{
Console.WriteLine("Creating registry...");
// Create a subkey named Test9999 under HKEY_CURRENT_USER.
string subKey;
Console.Write("Input registry sub key :");
subKey = Console.ReadLine();
RegistryKey testKey = Registry.CurrentUser.CreateSubKey(subKey);
Console.WriteLine("Created sub key {0}", subKey);
Console.WriteLine();

// Create two subkeys under HKEY_CURRENT_USER\Test9999. The
// keys are disposed when execution exits the using statement.
Console.Write("Input registry sub key 1:");
subKey = Console.ReadLine();
using (RegistryKey testKey1 = testKey.CreateSubKey(subKey))
{
testKey1.SetValue("name", "Justin");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
static void GetVersion(string nameToSearch)
{
// Get HKEY_LOCAL_MACHINE
RegistryKey baseRegistryKey = Registry.LocalMachine;

// If 32-bit OS
string subKey
//= "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
// If 64-bit OS
= "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
RegistryKey unistallKey = baseRegistryKey.OpenSubKey(subKey);

string[] allApplications = unistallKey.GetSubKeyNames();
foreach (string s in allApplications)
{
RegistryKey appKey = baseRegistryKey.OpenSubKey(subKey + "\\" + s);
string appName = (string)appKey.GetValue("DisplayName");
if(appName==nameToSearch)
{
string appVersion = (string)appKey.GetValue("DisplayVersion");
Console.WriteLine("Name:{0}, Version{1}", appName, appVersion);
break;
}


}

}

static void ListAll()
{
// Get HKEY_LOCAL_MACHINE
RegistryKey baseRegistryKey = Registry.LocalMachine;

// If 32-bit OS
string subKey
//= "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
// If 64-bit OS
= "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
RegistryKey unistallKey = baseRegistryKey.OpenSubKey(subKey);

string[] allApplications = unistallKey.GetSubKeyNames();
foreach (string s in allApplications)
{
RegistryKey appKey = baseRegistryKey.OpenSubKey(subKey + "\\" + s);
string appName = (string)appKey.GetValue("DisplayName");
string appVersion = (string)appKey.GetValue("DisplayVersion");
Console.WriteLine("Name:{0}, Version{1}", appName, appVersion);

}

}
}
} 

Next code base on similar solution is working for me:基于类似解决方案的下一个代码库对我有用:

var version = GetApplicationVersion("Windows Application Driver");

string GetApplicationVersion(string appName)
{
    string displayName;

    // search in: CurrentUser
    var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    foreach (var keyName in key.GetSubKeyNames())
    {
        var subKey = key.OpenSubKey(keyName);
        displayName = subKey.GetValue("DisplayName") as string;
        if (appName.Equals(displayName, StringComparison.OrdinalIgnoreCase))
            return subKey.GetValue("DisplayVersion").ToString();
    }

    // search in: LocalMachine_32
    key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");
    foreach (var keyName in key.GetSubKeyNames())
    {
        var subKey = key.OpenSubKey(keyName);
        displayName = subKey.GetValue("DisplayName") as string;
        if (appName.Equals(displayName, StringComparison.OrdinalIgnoreCase))
            return subKey.GetValue("DisplayVersion").ToString();
    }

    // search in: LocalMachine_64
    key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall");
    foreach (var keyName in key.GetSubKeyNames())
    {
        var subKey = key.OpenSubKey(keyName);
        displayName = subKey.GetValue("DisplayName") as string;
        if (appName.Equals(displayName, StringComparison.OrdinalIgnoreCase))
            return subKey.GetValue("DisplayVersion").ToString();
    }

    // NOT FOUND
    return null;
}

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

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