3

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);
      }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
tomohulk
  • 592
  • 2
  • 9
  • 22
  • 5
    You have two spaces in `Microsoft SQL Server` - is that intentional? – Oded Jun 17 '11 at 19:13
  • yes the key path is right, but is it maybe because of the first value name is (Default)? would that mess it up? – tomohulk Jun 17 '11 at 19:18
  • Now i see what you are talking about, that is just from the copy and past, my acutal code does not contain that. good eye. – tomohulk Jun 17 '11 at 19:22
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Jun 17 '11 at 19:23
  • Copy/Past does not add spaces. This is from _not_ using Copy/Paste, and that often leads to waste of time. – H H Jun 17 '11 at 20:00
  • the code editor on this site always re formats the layout of the my code. i wasn't talking about the copy and past from my IDE into here, but i do a lot of copy and pasting to get my code readable on this site. sorry for the confusion. – tomohulk Aug 19 '11 at 13:45

1 Answers1

6

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.

rid
  • 61,078
  • 31
  • 152
  • 193
  • Radu is right, it was looking in the wrong place, due to the WOW6432Node, the key im looking at exists in the default location, not in the WOW6432Node – tomohulk Jun 17 '11 at 19:34