Chirag Geiantilal
Chirag Geiantilal

Reputation: 31

Check if SQL Server is installed C#

I'm finishing a application in C# which contains a SQL Server database.

How can I check if the user has SQL Server 2012 Express Local DB installed?

Is it possible to check via registry both on x86, x64?

Basically the idea is if the user does not have SQL Server installed, the application advise to install it.

As the installer I'm working for setup does not have dependencies for SQL Server 2012 Express Local DB.

Thanks.

Upvotes: 0

Views: 1884

Answers (1)

JustBeingHelpful
JustBeingHelpful

Reputation: 18980

You'll have to loop through the Uninstall GUIDs and find one that starts with keyword "Microsoft SQL Server 2012". You can find it by going to Control Panel > Programs and Features > and look at the "Display Name" column.

//HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall{guidVariable}\DisplayName

.. should match the "Microsoft SQL Server 2012*" wild card.

I don't have the exact code, but this should get you started. Just loop through all children of the "Uninstall" key, and then find the "DisplayName" key by getting the value. The "GUID" variable below should be your iterator, since you do not know that value. I'm sure you can get a list of all of the GUID values that are sub keys of "Uninstall" key.

string UninstallRegKeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
Guid UninstallGuid = new Guid(GUID);

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(UninstallRegKeyPath, true))
{
    if (key == null)
    {
        return;
    }
    try
    {
        string guidText = UninstallGuid.ToString("B");
        RegistryKey child = key.OpenSubKey(guidText);
        if (child != null)
        {
        string displayName = child.GetValue("DisplayName").ToString();
        if (displayName.Contains("Microsoft SQL Server 2012"))
        {
            // implement logic when MSSQL 2012 is found
        }       
        child.Close();
        }
    }
}

Just be cautious. There are both 32 bit installations and 64 bit installations. Wow6432Node contains the 32 bit programs I believe, so everything installed in C:\Program Files (x86)\ by default (but may be anywhere). And there is the other location, which I'll let you find, for all of the 64 bit programs, which are installed in C:\Program Files\ by default (and again may be installed anywhere).

EDIT:

Try this. You may also want to replace "LocalMachine" with "CurrentUser" since many installers let you configure them for your user, or all users.

        using (RegistryKey root = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
        {
            string searchKey = @"Microsoft SQL Server 2012";
            string subKeyName = "DisplayName";

            foreach (string keyname in root.GetSubKeyNames())
            {
                //Console.WriteLine(keyname);
                using (RegistryKey key = root.OpenSubKey(keyname))
                {
                    try  // in case "DisplayName doesn't exist
                    {
                        string displayName = key.GetValue(subKeyName).ToString();
                        if (displayName.StartsWith(searchKey))
                            Console.WriteLine("GUID: " + keyname + Environment.NewLine + displayName + Environment.NewLine);
                    }
                    catch
                    {

                    }

                }
            }
        }

        Console.ReadLine();

Upvotes: 3

Related Questions