Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null Reference Exception with .GetValueNames() method

Tags:

c#

.net

registry

I am trying to get all the instance names of SQL on a machine, all the values are held in a regkey here is my code, but I keep getting a null reference exception.

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);
      }
}
like image 301
tomohulk Avatar asked Aug 29 '26 11:08

tomohulk


1 Answers

If you receive that exception, it means that key contains a null value. Therefore, the OpenSubKey() method did not return anything, likely because what you're searching for cannot be found.

like image 187
rid Avatar answered Aug 31 '26 01:08

rid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!