Useing Registry value to launch a application? whats wrong with it? .net 2.0 ONLY please
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\");
Cope开发者_StackOverflow社区n.GetValue("InstallProductPath");
System.Diagnostics.Process.Start(Copen + "cfp.exe");
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
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");
精彩评论