9

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

4
  • What do you want to accomplish with this? Depending on what you want, the installation path may not be enough. Commented Jul 22, 2009 at 14:16
  • Actually I need full path to the exe. Commented Jul 22, 2009 at 14:51
  • Which EXE? There are several, and what do you need it for? Again, depending on your reason, there may be a better way to do it. Commented Jul 22, 2009 at 16:40
  • here is a similar question for C++ stackoverflow.com/questions/1204920/… Commented Jul 31, 2009 at 15:52

3 Answers 3

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;
                }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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
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

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.