How do I get the installation path for a given instance of SQL Server (default and name instances)
Asked
Active
Viewed 1.2k times
9
-
What do you want to accomplish with this? Depending on what you want, the installation path may not be enough. – John Saunders Jul 22 '09 at 14:16
-
Actually I need full path to the exe. – Julius A Jul 22 '09 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. – John Saunders Jul 22 '09 at 16:40
-
here is a similar question for C++ http://stackoverflow.com/questions/1204920/how-to-get-the-sqlserver-installationpath-using-c/1205137#1205137 – Bogdan_Ch Jul 31 '09 at 15:52
3 Answers
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
-
2If 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