注册表信息(安装包ProductCode,设置启动运行)

时间:2023-03-09 18:04:54
注册表信息(安装包ProductCode,设置启动运行)

一、获取安装包ProductCode后,再获取安装包DisplayVersion,比对安装包版本,确定是否更新当前应用(重新下载安装包,并运行安装包)

     //获取当前应用程序的安装包的ProductCode属性值 (更多信息请查看注册表)
   public static string GetProductCode(string productName)
{
string productCode = string.Empty;

string x86 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; // 如果是32位操作系统,(或者系统是64位,程序也是64位)
string x64 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; // 如果操作系统是64位并且程序是32位的

RegistryKey keyUninstall = Registry.LocalMachine.OpenSubKey(x86, true);
if (keyUninstall == null) keyUninstall = Registry.LocalMachine.OpenSubKey(x64, true); //32位节点找不到则找64位节点

foreach (string subkey in keyUninstall.GetSubKeyNames())
{
if (!subkey.StartsWith("{") || !subkey.EndsWith("}")) continue;
RegistryKey key = keyUninstall.OpenSubKey(subkey);
string _displayname = key.GetValue("DisplayName") == null ? string.Empty : key.GetValue("DisplayName").ToString();
if (_displayname.StartsWith(productName)) productCode = subkey;
}
return productCode;
}
//安装新的安装包完成更新,如果完整文件名有空格,完整文件名使用双引号

//Using System.Diagnostics

Process.Start("msiexec", "/passive /i 安装包完整文件名.msi");