使用C#获取应用程序的安装版本 [英] get installed version of an application using c#

查看:478
本文介绍了使用C#获取应用程序的安装版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用C#获得应用程序(例如MyApp)的安装版本. 我会做的很多 1.为版本5.6的MyApp创建一个设置" 2.安装MyApp.

我将创建另一个应用程序(例如VersionTracker)以获取已安装应用程序的版本.因此,如果我传递名称"MyApp",则我希望将其命名为"5.6".如果另一个应用程序说我的系统中安装了Adobe Reader,则如果我通过"Adob​​e Reader",我想获得Adobe Reader的版本.

我需要知道如何构建'VersionTracker'

解决方案

第一个也是最重要的一点是,并非所有应用程序都确实将其版本保存在系统中的某个位置.老实说,只有少数几个这样做.您应该看的地方是Windows注册表.大多数已安装的应用程序将其安装数据放在以下位置:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

但是,这并不容易-在64位Windows上,32位(x86)应用程序将其安装数据保存到另一个密钥中,即:

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

在这些键中,有许多键,其中一些具有易于阅读"的名称,例如Google Chrome,其中一些具有例如{63E5CDBF-8214-4F03-84F8-CD3CE48639AD}的名称.您必须将所有这些键解析到您的应用程序中,然后开始寻找应用程序名称. DisplayName值通常存在,但并非总是如此.应用程序的版本通常为DisplayVersion值,但是某些安装程序的确使用其他值,例如Inno Setup: Setup Version,....某些应用程序的名称确实以其名称写成,因此该应用程序的版本可能已经在DisplayName值中.

注意:解析所有这些注册表项和值并挑选"正确的值并不容易.并非所有安装程序都将应用程序数据保存到这些注册表项中,其中一些没有将应用程序版本保存在这些注册表项中,等等.但是,通常应用程序使用这些注册表项. [来源: StackOverflow:通过注册表检测已安装的程序,浏览我自己的注册表]

好的,所以现在当您知道应该在哪里看时,就必须使用C#对其全部进行编程.我不会为您编写应用程序,但会告诉您应该使用哪些类以及如何使用.首先,您需要这些:

using System;
using Microsoft.Win32;      

要进入您的HKEY_LOCAL_MACHINE,请像这样创建一个RegistryKey:

RegistryKey baseRegistryKey = Registry.LocalMachine;

现在您需要定义子项:

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

现在您需要转到子项,因此创建一个新的RegistryKey:

RegistryKey uninstallKey = baseRegistryKey.OpenSubKey(subKey);

现在您需要遍历那里的所有子项,因此首先我们获得所有子项的名称:

string[] allApplications = uninstallKey.GetSubKeyNames();

现在您必须自己创建所有注册表项,一个个地遍历所有子项(您不必这样做,但我会这样做):

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

其中,applicationSubKeyName是您当前正在检查的子项的名称.我建议使用foreach语句,这对您有帮助(但是您必须已经具有C#的使用经验,在这里我不会告诉您如何使用foreach).

现在检查应用程序的名称并将其与所需应用程序的名称进行比较(您不能依赖子键名称,因为正如我已经说过的那样,可以将它们称为例如{63E5CDBF-8214-4F03-84F8-CD3CE48639AD},因此您必须在此处检查名称):

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

如果它是正确的应用程序(必须自己检查),请查找版本:

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

等等,您有版本.至少有60%-80%的机会...

记住!!如果某些键或值不存在,该方法将返回null.请记住每次都要检查返回的值是否为null,否则您的应用程序将崩溃.

在哪里可以找到更多? 代码项目:使用C#从注册表中读取,写入和删除

我真的希望我能对您有所帮助.如果您想了解其他信息,但我不明白您的问题,请下次再问. :)

I would to get installed version of an application (say, MyApp) using C#. I will do this much, 1. Create a 'Set Up' for MyApp of version 5.6 2. Install MyApp.

I will create another application (say VersionTracker)to get the version of installed applications. So if I pass the name 'MyApp' I would like to get the version as '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'.

I need to know how to build '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. 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:

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}. 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. 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.

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]

Alright, so now when you know where you should look, you have to program it all in 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:

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:

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:

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):

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

where applicationSubKeyName is the name of the subkey you're currently checking. 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).

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):

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...

Remember! If some key or value doesn't exist, the method returns null. Remember to check if the returned value is null everytime, otherwise your application will crash.

Where to find more? The Code Project: Read, write and delete from registry with 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. :)

这篇关于使用C#获取应用程序的安装版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆