当 ClickOnce 应用程序按用户安装时,您可以通过以下注册表中的路径找到卸载信息(应用程序向导向您显示的内容):
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Uninstall\5f7eb300e2ea4ebf
此“卸载”具有唯一的哈希子键,要查找您的应用程序,您可以遍历这些键并过滤,例如 DisplayName,如下所示:
private RegistryKey GetUninstallRegistryKeyByProductName(string productName)
{
var subKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");
if (subKey == null)
return null;
foreach (var name in subKey.GetSubKeyNames())
{
var application = subKey.OpenSubKey(name, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.QueryValues | RegistryRights.ReadKey | RegistryRights.SetValue);
if (application == null)
continue;
foreach (var appKey in application.GetValueNames().Where(appKey => appKey.Equals("DisplayName")))
{
if (application.GetValue(appKey).Equals(productName))
return application;
break;
}
}
return null;
}
此方法返回 RegistryKey,然后您可以获取 'DisplayVersion' 键值:
var key = GetUninstallRegistryKeyByProductName("myApp");
var version = key.GetValue("DisplayVersion");
更新
关于安装日期,尝试获取注册表项的最后写入时间(获取“DisplayVersion”的最后写入时间是您所需要的)。看起来没有托管包装器可以获取此信息,因此请使用 P/Invoke。您需要调用 RegQueryInfoKey。