List the Installed Instances of SQL Server

February 12, 2016

The following code will list all the SQL Server instances on the local machine.



            RegistryView rv = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;

            using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, rv))
            {
                RegistryKey rk = hklm.OpenSubKey(@"SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL", false);
                if (rk == null) return;

                foreach(var v in rk.GetValueNames())
                {
                    Console.WriteLine($"{v} : {rk.GetValue(v)}");
                }
            }

            Console.ReadLine();

… assuming the registry is correct



Profile picture

A blog about one man's journey through code… and some pictures of the Peak District
Twitter

© Paul Michaels 2024