问题描述
我使用从 Windows安装程序API的C#包装。我用的是 ProductInstallation
类,以获取有关安装的产品信息,如产品代码和产品名称。
I am using the C# wrapper for the Windows Installer API from the WIX Toolset. I use the ProductInstallation
class to get information about the installed products such as the product code and product name.
例如
- 产品名称 - 我的测试应用程序李>
- 产品编号 - {F46BA620-C027-4E68-9069-5D5D4E1FF30A}
- 产品版本 - 1.4.0
在内部此包装采用的的功能。不幸的是这个函数不返回该产品的升级代码。
Internally this wrapper uses the MsiGetProductInfo function. Unfortunately this function does not return the product's upgrade code.
我如何使用C#检索升级代码安装应用程序?
How can I retrieve the upgrade code for an installed application using C#?
推荐答案
我发现升级代码存储在以下注册表位置。
I have discovered the upgrade codes are stored in the following registry location.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes
注册表项名称是升级代码和注册表项值的产品代码。我可以很容易地提取这些值但是代码存储在不同的格式。
The registry key name is the upgrade code and the registry key value is the product code. I can easily extract these values however the codes are stored in a different format.
的连字符被剥离出的的Guid
然后一系列串逆转的完成。前8个字符被颠倒,然后在接下来的4,那么下面4,然后将字符串的其余部分在套2的字符是相反的。扭转一个字符串,通常当我们需要照顾在确保控制和特殊字符都正确处理(的),但因为我们是在这种情况下,处理一个的Guid
字符串,我们可以确信的字符串正确逆转。
The hyphens are stripped out of the Guid
and then a series of string reversals are done. The first 8 characters are reversed, then the next 4, then the following 4 and then the rest of the string is reversed in sets of 2 characters. Normally when reversing a string we need to take care in making sure control and special characters are handled correctly (see Jon Skeet's aricle here) but as we are, in this case, dealing with a Guid
string we can be confident the string will be reversed correctly.
下面是我用来提取从注册表中的一个已知的产品代码的升级代码的完整代码。
Below is the complete code I used to extract the upgrade code for a known product code from the registry.
internal static class RegistryHelper
{
private const string UpgradeCodeRegistryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes";
private static readonly int[] GuidRegistryFormatPattern = new[] { 8, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2 };
public static Guid? GetUpgradeCode(Guid productCode)
{
// Convert the product code to the format found in the registry
var productCodeSearchString = ConvertToRegistryFormat(productCode);
// Open the upgrade code registry key
var upgradeCodeRegistryRoot = Registry.LocalMachine.OpenSubKey(UpgradeCodeRegistryKey);
if (upgradeCodeRegistryRoot == null)
return null;
// Iterate over each sub-key
foreach (var subKeyName in upgradeCodeRegistryRoot.GetSubKeyNames())
{
var subkey = upgradeCodeRegistryRoot.OpenSubKey(subKeyName);
if (subkey == null)
continue;
// Check for a value containing the product code
if (subkey.GetValueNames().Any(s => s.IndexOf(productCodeSearchString, StringComparison.OrdinalIgnoreCase) >= 0))
{
// Extract the name of the subkey from the qualified name
var formattedUpgradeCode = subkey.Name.Split('\\').LastOrDefault();
// Convert it back to a Guid
return ConvertFromRegistryFormat(formattedUpgradeCode);
}
}
return null;
}
private static string ConvertToRegistryFormat(Guid productCode)
{
return Reverse(productCode, GuidRegistryFormatPattern);
}
private static Guid ConvertFromRegistryFormat(string upgradeCode)
{
if (upgradeCode == null || upgradeCode.Length != 32)
throw new FormatException("Product code was in an invalid format");
upgradeCode = Reverse(upgradeCode, GuidRegistryFormatPattern);
return Guid.Parse(upgradeCode);
}
private static string Reverse(object value, params int[] pattern)
{
// Strip the hyphens
var inputString = value.ToString().Replace("-", "");
var returnString = new StringBuilder();
var index = 0;
// Iterate over the reversal pattern
foreach (var length in pattern)
{
// Reverse the sub-string and append it
returnString.Append(inputString.Substring(index, length).Reverse().ToArray());
// Increment our posistion in the string
index += length;
}
return returnString.ToString();
}
}
这篇关于我如何才能找到在C#中安装应用程序的升级代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!