How do I get the installation path for a given instance of SQL Server (default and name instances)
+2
A:
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;
}
}
}
}
}
J Angwenyi
2009-07-22 14:47:52
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
2009-07-22 16:42:58
A:
Hi Jangwenyi and John,
I was a very helpful post. It is exactly what I was looking for. I have implemented this and It works like charm.......!!!
Thank you
Kruti Shukla