How do I get the installation path for a given instance of SQL Server (default and name instances)
-
What do you want to accomplish with this? Depending on what you want, the installation path may not be enough.John Saunders– John Saunders2009-07-22 14:16:16 +00:00Commented Jul 22, 2009 at 14:16
-
Actually I need full path to the exe.Julius A– Julius A2009-07-22 14:51:07 +00:00Commented 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.John Saunders– John Saunders2009-07-22 16:40:02 +00:00Commented Jul 22, 2009 at 16:40
-
here is a similar question for C++ stackoverflow.com/questions/1204920/…Bogdan_Ch– Bogdan_Ch2009-07-31 15:52:25 +00:00Commented Jul 31, 2009 at 15:52
Add a comment
|
3 Answers
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;
}
}
}
}
}
2 Comments
John Saunders
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.
Alex Klaus
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
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
how-to-find-all-sql-server-instance-running-in-local-network