I am trying to get all the SQL Instances installed on a local machine with the help of following code:
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
string[] instances = (string[])rk.GetValue("InstalledInstances");
if (instances.Length > 0) //Error
{
foreach (string instance in instances)
{
MessageBox.Show(instance);
}
}
It is giving the following exception:
Object reference not set to an instance of an object.
I've manually checked the 'Registry-Editor' and the specified keys exist, also there are two SQL Instances on my system.
Now please tell me why it is not accessing the required key value?