获取 C# 中某个程序的卸载命令



所以我正在处理这段代码,它返回一个卸载命令字符串来卸载某个程序。 我在其他问题中看到过这段代码,但似乎没有人遇到与我相同的问题。 这是代码:

public static string GetUninstallCommandFor(string productDisplayName)
{
RegistryKey localMachine = Registry.LocalMachine;
string productsRoot = @"SOFTWAREMicrosoftWindowsCurrentVersionInstallerUserDataS-1-5-18Products";
RegistryKey products = localMachine.OpenSubKey(productsRoot);
string[] productFolders = products.GetSubKeyNames();
foreach (string p in productFolders)
{
RegistryKey installProperties = products.OpenSubKey(p + @"InstallProperties");
if (installProperties != null)
{
string displayName = (string)installProperties.GetValue("DisplayName");
if ((displayName != null) && (displayName.Contains(productDisplayName)))
{
string uninstallCommand = (string)installProperties.GetValue("UninstallString");
return uninstallCommand;
}
}
}
return "";
}

此代码返回此错误:

System.NullReferenceException:"对象引用未设置为对象的实例。

产品为空。

我不知道产品怎么可能是空的,我确实检查了那个子项,里面装满了文件夹,所以我该如何解决这个问题。

为 X64 构建。 在 32 位进程中,您实际上是通过注册表重定向读取HKEY_LOCAL_MACHINESoftwareWow6432Node

或者,您可以根据以下条件请求注册表的未重定向视图: 避免注册表 Wow6432节点重定向

最新更新