我正在使用WIX Toolset中的Windows Installer API的C#包装器。我使用ProductInstallation
类来获取有关已安装产品的信息,例如产品代码和产品名称。
例如
这个包装器在内部使用MsiGetProductInfo函数。不幸的是,此功能不会返回产品的升级代码。
如何使用C#检索已安装应用程序的升级代码?
答案 0 :(得分:30)
我发现升级代码存储在以下注册表位置。
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes
注册表项名称是升级代码,注册表项值名称是产品代码。我可以轻松提取这些值,但代码以不同的格式存储。红色圆圈显示格式化的升级代码,蓝色圆圈在regedit.exe
中查看时显示格式化的产品代码。
将连字符从Guid
中删除,然后进行一系列字符串反转。前8个字符相反,然后是接下来的4个字符,然后是下面的4个字符,然后字符串的其余部分以2个字符的形式反转。通常在反转字符串时,我们需要注意确保正确处理控制和特殊字符(see Jon Skeet's aricle here)但是在这种情况下,处理Guid
字符串我们可以确信字符串将被正确反转。
以下是我用于从注册表中提取已知产品代码的升级代码的完整代码。
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 localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
var upgradeCodeRegistryRoot = 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();
}
}
答案 1 :(得分:5)
InstallPackage类有一个名为LocalPackage的属性。您可以使用它来查询在C:\ Windows \ Installer中缓存的MSI数据库,并获取您可能想要了解的任何信息。
答案 2 :(得分:3)
答案 3 :(得分:2)
这是从UpgradeCode获取ProductCode的相反方法。可能对某人有用。
using Microsoft.Win32;
using System;
using System.IO;
using System.Linq;
using System.Text;
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? GetProductCode(Guid upgradeCode)
{
// Convert the product code to the format found in the registry
var productCodeSearchString = ConvertToRegistryFormat(upgradeCode);
// Open the upgrade code registry key
var upgradeCodeRegistryRoot = GetRegistryKey(Path.Combine(UpgradeCodeRegistryKey, productCodeSearchString));
if (upgradeCodeRegistryRoot == null)
return null;
var uninstallCode = upgradeCodeRegistryRoot.GetValueNames().FirstOrDefault();
if (string.IsNullOrEmpty(uninstallCode))
{
return null;
}
// Convert it back to a Guid
return ConvertFromRegistryFormat(uninstallCode);
}
private static string ConvertToRegistryFormat(Guid code)
{
return Reverse(code, GuidRegistryFormatPattern);
}
private static Guid ConvertFromRegistryFormat(string code)
{
if (code == null || code.Length != 32)
throw new FormatException("Product code was in an invalid format");
code = Reverse(code, GuidRegistryFormatPattern);
return Guid.Parse(code);
}
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();
}
static RegistryKey GetRegistryKey(string registryPath)
{
var hklm64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
var registryKey64 = hklm64.OpenSubKey(registryPath);
if (((bool?)registryKey64?.GetValueNames()?.Any()).GetValueOrDefault())
{
return registryKey64;
}
var hklm32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
return hklm32.OpenSubKey(registryPath);
}
}
答案 4 :(得分:1)
这里有一种更简单的方法来获取注册表格式的 GUID 格式(基本上只是一个原始字节表示)
首先是获取原始字节:
#For simplicity reasons just three files - you can use a generator or read the filenames from the directory
files = ['file1.json', 'file2.json', 'file3.json']
for file in files:
f = open(file)
#Do your stuff with the file
f.close()
然后只需翻转 BitConverter.ToString() 结果的字节序
var guidBytes = Guid.Parse(productCode).ToByteArray();
答案 5 :(得分:0)
这是你的帮助程序修改,它也适用于.Net3.5 32位应用程序。他们需要特殊的处理,因为.net 3.5没有意识到注册表在32位和64位之间被分割。我的解决方案是仅使用To64BitPath
来浏览64位部分。还有一个很棒的教程,使用DllImports:https://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/
class RegistryHelper
{
private const string UpgradeCodeRegistryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes";
private const string UninstallRegistryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
private static readonly int[] GuidRegistryFormatPattern = new[] { 8, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2 };
public static string To64BitPath(string path)
{
return path.Replace("SOFTWARE\\Microsoft", "SOFTWARE\\WOW6432Node\\Microsoft");
}
private static RegistryKey GetLocalMachineRegistryKey(string path)
{
return RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, string.Empty).OpenSubKey(path);
}
public static IEnumerable<Guid> GetUpgradeCodes()
{
var list = new List<Guid>();
var key = GetRegistryKey(UpgradeCodeRegistryKey);
if (key != null)
{
list.AddRange(key.GetSubKeyNames().Select(ConvertFromRegistryFormat));
}
return list;
}
public static Guid? GetProductCode(Guid upgradeCode)
{
// Convert the product upgradeCode to the format found in the registry
var productCodeSearchString = ConvertToRegistryFormat(upgradeCode);
// Open the upgradeCode upgradeCode registry key
var upgradeCodeRegistryRoot = GetRegistryKey(Path.Combine(UpgradeCodeRegistryKey, productCodeSearchString));
if (upgradeCodeRegistryRoot == null)
return null;
var uninstallCode = upgradeCodeRegistryRoot.GetValueNames().FirstOrDefault();
if (string.IsNullOrEmpty(uninstallCode))
{
return null;
}
// Convert it back to a Guid
return ConvertFromRegistryFormat(uninstallCode);
}
public static string ConvertToRegistryFormat(Guid code)
{
return Reverse(code, GuidRegistryFormatPattern);
}
private static Guid ConvertFromRegistryFormat(string code)
{
if (code == null || code.Length != 32)
throw new FormatException("Product upgradeCode was in an invalid format");
code = Reverse(code, GuidRegistryFormatPattern);
return new Guid(code);
}
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();
}
static RegistryKey GetRegistryKey(string registryPath)
{
var registryKey64 = GetLocalMachineRegistryKey(To64BitPath(registryPath));
if (((bool?)registryKey64?.GetValueNames()?.Any()).GetValueOrDefault())
{
return registryKey64;
}
return GetLocalMachineRegistryKey(registryPath);
}
public static Guid? GetUpgradeCode(Guid productCode)
{
var productCodeSearchString = ConvertToRegistryFormat(productCode);
var upgradeCodeRegistryRoot = GetRegistryKey(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 upgradeCode
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;
}
}