0

I need to get the folder with full path of my SQL Server instance installed on my machine, using vb code.

For example, I have installed instance MyComputer\MyInstanceName.

I know it is in C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL but how can I get this path using vb.net code?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Furqan Sehgal
  • 11
  • 1
  • 5
  • For this you need to access registry values using VB.NET. See this: http://stackoverflow.com/questions/1165576/c-sharp-how-to-get-sql-server-installation-path-programatically – Harsh Sep 06 '15 at 08:32

1 Answers1

1

It's not all that obvious, but you can find everything you're looking for in the registry.

Basically:

  • you need to inspect the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL subkey to find the installed instances and their "internal" names

  • with that internal name, you can inspect the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\(internal name)\Setup node to find everything you might want to know about that instance

In C# code (and for a 64-bit OS, with 64-bit versions of SQL Server), this would be:

// open the 64-bit view of the registry, if you're using a 64-bit OS
RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

// find the installed SQL Server instance names
RegistryKey key = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL");

// loop over those instances
foreach (string sqlInstance in key.GetValueNames())
{
    Console.WriteLine("SQL Server instance: {0}", sqlInstance);

    // find the SQL Server internal name for the instance
    string internalName = key.GetValue(sqlInstance).ToString();
    Console.WriteLine("\tInternal instance name: {0}", internalName);

    // using that internal name - find the "Setup" node in the registry
    string instanceSetupNode = string.Format(@"SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup", internalName);

    RegistryKey setupKey = baseKey.OpenSubKey(instanceSetupNode, false);

    if (setupKey != null)
    {
        // in the "Setup" node, you have several interesting items, like
        // * edition and version of that instance
        // * base path for the instance itself, and for the data for that instance
        string edition = setupKey.GetValue("Edition").ToString();
        string pathToInstance = setupKey.GetValue("SQLBinRoot").ToString();
        string version = setupKey.GetValue("Version").ToString();

        Console.WriteLine("\tEdition         : {0}", edition);
        Console.WriteLine("\tVersion         : {0}", version);
        Console.WriteLine("\tPath to instance: {0}", pathToInstance);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459