tags:

views:

89

answers:

2

I am trying to write a little piece of code that retrieves the install path of an application, and uses it + the applications name to start the application on click. This is what I have so far but I keep getting the error "Object reference not set to an instance of an object". This is the code I'm trying to use, whats wrong?

RegistryKey Copen = Registry.LocalMachine.OpenSubKey(@"Software\ComodoGroup\CDI\1\");
Copen.GetValue("InstallProductPath");
System.Diagnostics.Process.Start(Copen + "cfp.exe");
A: 

Your call to Registry.LocalMachine.OpenSubKey or GetValue might fail and then it returns null. Then when they are used you will get the null reference exception.

Try to see if any of the values returned by these methods are null. Something like this:

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\ComodoGroup\CDI\1");
if (key != null)
{
   object = installProductPath = key.GetValue("InstallProductPath");
   // You could also supply a default value like this:
   // installProductPath = key.GetValue("InstallProductPath", @"C:\The\Default\Path");
   if (installProductPath != null)
   {
       System.Diagnostics.Process.Start(Path.Combine(installProductPath.ToString() + "cfp.exe"); 
   }
}

Edit

Guess you just wrote this line wrong, but you are not supplying the value, but the RegistryKey value:

System.Diagnostics.Process.Start(Copen + "cfp.exe");
Martin Ingvar Kofoed Jensen
+2  A: 

You're not actually storing the value you're retrieving. Try this:

RegistryKey Copen = Registry.LocalMachine.OpenSubKey(@"Software\ComodoGroup\CDI\1\", RegistryKeyPermissionCheck.ReadSubTree);
if(Copen != null)
{
    object o = Copen.GetValue("InstallProductPath");
    if(o != null)
    {
         System.Diagnostics.Process.Start(IO.Path.Combine(o.ToString(), "cfp.exe"));
    }
    else
        MessageBox.Show("Value not found");
}
else
    MessageBox.Show("Failed to open key");

Edited: to also check for NULL as Martin mentioned

ho1
Doesnt seem to do any thing, it compiles ok but when i click it, it does nothing.
NightsEVil
@NightsEvil: Try now and it'll hopefully tell you why it's failing.
ho1
says "Failed to open key" ... but it does exist, could it be because its in localmachine?
NightsEVil
@NightsEvil: Are you running the app as someone with Admin rights? Otherwise, try passing in `RegistryKeyPermissionCheck.ReadSubTree` as a second optional parameter to the `OpenSubKey`. You could also try `if(null == Registry.LocalMachine.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadSubTree)) MessageBox.Show("Problem");` and then add on more bits of the path to see which bits works.
ho1
well my user is the admin but it will be running on a computer that may not be admin (separate question can i make it automatically force its self to have admin rights?). ill try to try your suggestion if i can figure it out im still quite a novice and still learning so try to bare with me :) UPDATE!: it works PERFECTLY when trying to access a value in Current User! now how to make it so it works in localmachine?
NightsEVil
@NightsEvil: If you do open it as read-only you should be able to read from HKLM as well even if not admin. If you do want to elevate your app to have admin rights, look at this link for some info: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/52640035-b12c-4332-91a6-aca12bd26b20
ho1
how do i make sure its doing read only?
NightsEVil
@NightsEvil: Send in either `RegistryKeyPermissionCheck.ReadSubTree` or `false` as the second parameter.
ho1
can you put that into your example please so i can see how it would look?
NightsEVil
@NightsEvil: Updated my answer slightly to include that.
ho1
still getting "Failed to open key"
NightsEVil
@NightsEvil: Try changing `RegistryKeyPermissionCheck.ReadSubTree` to `false` and see if it makes any difference. Also make sure that the path is exactly right in the registry (you might also want to go in with Regedit.exe and check that there are no other permission problems to access the key in question).
ho1
already tried changing it to false, same thing "Failed to open key" and iv checked the key path and its perfectly fine, it shouldnt matter if it has or doesnt have the slash at the end right? like "Software\ComodoGroup\CDI\1\" or "Software\ComodoGroup\CDI\1"?
NightsEVil
@NightsEvil: Not sure, try it both ways. And try just `Software` then `Software\ComodoGroup` etc and you can maybe find where it breaks.
ho1
having any luck? cause im not at all ... lol
NightsEVil