Tag Archives: RegistryKey

List the Installed Instances of SQL Server

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

Get the executable location of SQLPackage

In my recent escapades into the murky world of deploying dacpac files into a SQL DB, it occurred to me that the location of the SQLPackage file has largely been a cut-and-paste from the web. Apparently, the registry key that holds this information is: HKLM\SOFTWARE\Microsoft\Microsoft SQL Server\Data-Tier Application Framework\InstallLocation. Here’s some code to retrieve it using C#:

        private static void GetSQLDacTools()
        {
            using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, _registryView))
            {
                RegistryKey rk = hklm.OpenSubKey(
                    @"SOFTWARE\Microsoft\Microsoft SQL Server\Data-Tier Application Framework\InstallLocation");
                if (rk == null)
                {
                    Console.WriteLine("Sql Data Tools Not Installed");
                    return;
                }

                string path = rk.GetValue(string.Empty).ToString();

                Console.WriteLine(path);
            }            
        }