更新操作系统时,MSI无法访问注册表 [英] MSI is not able to access registry when I update OS
问题描述
你好,
我创建了一个MSI项目。我的目的是在安装时创建注册表并为它们分配一些值。我正在HKEY_CURRENT_USER \Software下创建注册表。我有一些默认的注册表集,我使用解决方案资源管理器中的注册表编辑器创建。我还有一个安装程序类,我根据某些条件创建了一些更多的注册表。这是我的相同代码片段:
Hello,
I have created an MSI project. My purpose is to create registries and assign some values to them while installing. I am creating registry under HKEY_CURRENT_USER\Software. I have some default set of registries that I have created using Registry Editor from solution explorer. Also I have an installer class using which I have created some more registries based on some conditions. Here is my code snippet for the same:
If Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\ABCD\DB").GetValue(RegName) Is Nothing Then
Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\ABCD\DB", True).SetValue(RegName, RegVal)
End If
这里RegName是要创建的注册表名称,RegVal是要为RegName Registry设置的值。
当我安装此MSI时,我在设计时使用注册表编辑器使用解决方案资源管理器创建的注册表创建并分配成功的价值观。但问题是由安装程序类创建的注册表,而不是创建。当我试图检查天气RegName什么都不是时,它会引发异常。抛出的异常是:对象未设置为对象的实例。实际上它适用于没有Windows更新的所有机器。更新计算机操作系统时会出现此问题。我不知道究竟是什么问题。为什么更新机器失败?我已经尝试了很多,但无法得到任何适当的解决方案。另外一件事,问题出现在Windows 10上。当我设置MSI为所有用户运行时,它将给出此异常。如果我选择为Just me选项运行MSI,MSI将正常工作。
等待适当的建议。
快速反应非常明显。
谢谢,
Ankit
Here RegName is the Registry name to be created and RegVal is the value to be set for RegName Registry.
When I am installing this MSI, registries, which I have created at the design time by using Registry Editor using Solution Explorer, create and assign values to them successfully. But the issue is registries those are created by installer class, is not creating. It throws an exception when I tried to check weather RegName is nothing or not. Thrown exception is : "Object is not set to instance of an object". Actually it works fine for all the machine which are not having Windows update. This behaviour occurs when machine OS is updated. I dont know what is an exact issue. Why does it fail for updated machine? I have tried a lot but cant get any proper solution. Also one thing, the issue is occurring on Windows 10. When I set MSI to run for the "All user" it will give this exception. MSI will work fine if I select run MSI for "Just me" option.
Waiting for appropriate suggestions.
Quick response are highly appreciable.
Thanks,
Ankit
推荐答案
如果指定的密钥不存在,OpenSubKey
方法 [ ^ ]将返回Nothing
。然后,您的代码会尝试调用GetValue
方法 [ ^ ]返回值,导致NullReferenceException
。
如果您使用CreateSubKey
方法 [ ^ ]如果它不存在,它将创建密钥:
If the specified key doesn't exist, theOpenSubKey
method[^] will returnNothing
. Your code then attempts to call theGetValue
method[^] on the returned value, which results in aNullReferenceException
.
If you use theCreateSubKey
method[^] instead, it will create the key if it doesn't exist:
Using key As RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\ABCD\DB", True)
If key Is Nothing Then
Throw New InvalidOperationException("Failed to create the registry key.")
End If
If key.GetValue(RegName) Is Nothing Then
key.SetValue(RegName, RegVal)
End If
End Using
这篇关于更新操作系统时,MSI无法访问注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!