0

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.

4
  • Possible duplicate of Check if SQL Server is installed on a machine through C# Commented Feb 13, 2016 at 3:05
  • Already tried, not working. The application is 32 bits and OS 64 bits. Commented Feb 13, 2016 at 3:09
  • Not a direct answer, but would it not be better to stored application data in SQLite instead? Commented Feb 14, 2016 at 1:09
  • 1
    That's a good point, thanks. Commented Feb 14, 2016 at 12:37

1 Answer 1

3

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

2 Comments

Ugly as hell, but the best approach possible. Nice - will have to remember that ;)
Thanks, will try this.

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.