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

1

1 Answer 1

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

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.