2011-07-02 87 views
2

我将使用C#获取应用程序的安装版本(比如MyApp)。 我会做这么多, 1.为5.6版MyApp创建'设置' 2.安装MyApp。使用C#获取已安装的应用程序版本

我将创建另一个应用程序(说VersionTracker)来获取已安装的应用程序的版本。所以如果我通过'MyApp'这个名字,我想把版本设置为'5.6'。如果另一个应用程序说我的系统中安装了Adobe Reader,如果我通过'Adobe Reader',我想要得到Adobe Reader的版本。

我需要知道如何建立“VersionTracker”

+2

我们需要更多的细节。你在用什么工具,你到底想要做什么,你尝试过什么? –

+1

我同意。这个问题非常模糊。换句话说,跆拳道你在说什么? –

+0

您将名称“MyApp”传递到哪里?在应用程序本身?在Windows的其他部分?你把这个名字放在哪里?一些搜索? –

回答

12

第一和最重要的一点是,并非所有应用程序都保存在某个地方的版本在系统中。说实话,只有少数人这样做。你应该看的地方是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: Detecting installed programs via registry,浏览我自己的注册表]

好的,所以现在当你知道你应该看的地方时,你必须用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"); 

的Et瞧,你拥有的版本。至少有60-80%的机会你有...

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

哪里可以找到更多? The Code Project: Read, write and delete from registry with C#

我真的很希望我帮你。如果你想知道其他的东西,但是我不明白你的问题,那么请下次再提问。 :)

+0

我创建了一个小应用程序,代码可以在http://rauf-thecoder.blogspot.com/2011/07/registry-in-c.html –

+1

找到Quote:“我不会为你写应用程序。 ..“ - 哦,确定你做到了。 :) –

2
/// 
/// 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); 

} 

} 
} 
} 
+0

你至少可以缩进你的代码。 – Torxed

相关问题