Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

VB
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

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
Posted
Comments
Richard MacCutchan 7-Aug-15 8:03am    
Most likely because the key does not exist. You need to create it first.

1 solution

If the specified key doesn't exist, the OpenSubKey method[^] will return Nothing. Your code then attempts to call the GetValue method[^] on the returned value, which results in a NullReferenceException.

If you use the CreateSubKey method[^] instead, it will create the key if it doesn't exist:
VB.NET
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900