9

How do I get the installation path for a given instance of SQL Server (default and name instances)

Julius A
  • 38,062
  • 26
  • 74
  • 96

3 Answers3

7
using(RegistryKey sqlServerKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server"))
{
    foreach (string subKeyName in sqlServerKey.GetSubKeyNames())
    {
        if(subKeyName.StartsWith("MSSQL."))
        {
            using(RegistryKey instanceKey = sqlServerKey.OpenSubKey(subKeyName))
            {
                string instanceName = instanceKey.GetValue("").ToString();

                if (instanceName == "MSSQLSERVER")//say
                {
                    string path = instanceKey.OpenSubKey(@"Setup").GetValue("SQLBinRoot").ToString();
                    path = Path.Combine(path, "sqlserver.exe");
                    return path;
                }
            }
        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Julius A
  • 38,062
  • 26
  • 74
  • 96
  • Of course, this depends on how SQL Server chooses to use registry keys, and will break as soon as they change that. I'm sure you'll admit that Microsoft has the right to change their own registry keys? That's why I asked you what you're trying to accomplish. There may be a way to do it that won't break from release to release. – John Saunders Jul 22 '09 at 16:42
  • 2
    If you are looking for 32-bit instances on a 64-bit OS (pretty weird, but possible), you will need to look: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server – Alex Klaus Feb 26 '13 at 00:02
0

If you have the connection string, you may select the Directory with SQL

private string ServerRootDirectory(string connString)
    {
        string path = string.Empty;
        using (SqlConnection con = new SqlConnection(connString))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = string.Format(@"DECLARE @InstanceName varchar(100), 
                                                        @InstanceLocation varchar(100),
                                                        @InstancePath varchar(100)

                                                SELECT @InstanceName = convert(varchar, ServerProperty('InstanceName'))
                                                EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
                                                    @key='Software\Microsoft\Microsoft SQL Server\Instance Names\SQL',
                                                    @value_name=@InstanceName,
                                                    @value=@InstanceLocation OUTPUT
                                                SELECT @InstanceLocation = 'Software\Microsoft\Microsoft SQL Server\'+@InstanceLocation+'\Setup'

                                                EXEC master..xp_regread @rootkey='HKEY_LOCAL_MACHINE',
                                                    @key=@InstanceLocation,
                                                    @value_name='SQLPath',
                                                    @value=@InstancePath OUTPUT
                                                SELECT @InstancePath as RootDirectoryPath");
            path = (string)cmd.ExecuteScalar();
            con.Close();
        }
        return path;
    }

Output of the above code:

c:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL

George Stavrou
  • 482
  • 5
  • 11
-3

how-to-find-all-sql-server-instance-running-in-local-network

http://www.ehsanenaloo.com/index.php/200/programming/csharp/how-to-find-all-sql-server-instance-running-in-local-network-c.html

shamim
  • 6,640
  • 20
  • 85
  • 151