Null Reference exception using .GetValueNames () method

I try to get all the SQL instance names on the machine, all the values ​​are stored in regkey here, this is my code, but I continue to get an exception with a link to the link.

private void RegLoop()
{
     RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL")
      foreach (var v in key.GetValueNames())
      {
         MessageBox.Show("{0}", v);
      }
}
+3
source share
1 answer

If you get this exception, it means it keycontains a value null. Therefore, the method OpenSubKey()did not return anything, most likely because the search could not be found.

+6
source

All Articles