Use registry value to run application? what's wrong with that? .net 2.0 ONLY

I am trying to write a small piece of code that extracts the installation path of an application and uses its + application name to launch the application on click. This is what I still have, but I keep getting the error "The object reference is not installed in the object instance." This is the code I'm trying to use, what's wrong?

RegistryKey Copen = Registry.LocalMachine.OpenSubKey(@"Software\ComodoGroup\CDI\1\");
Copen.GetValue("InstallProductPath");
System.Diagnostics.Process.Start(Copen + "cfp.exe");
+3
source share
2 answers

In fact, you are not storing the value that you are retrieving. Try the following:

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: also for checking NULL, as Martin mentioned

+2
source

Registry.LocalMachine.OpenSubKey GetValue , null. , , .

, - , , null. - :

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

, , , RegistryKey:

System.Diagnostics.Process.Start(Copen + "cfp.exe");
0

Source: https://habr.com/ru/post/1757285/


All Articles