sreeee
sreeee

Reputation: 41

How do I detect whether MySQL is installed?

Without the help of the registry, how do I know whether MySQL is installed or not? I am trying to determine this on a Windows machine through C#.

I have found a solution that involves querying the registry, but I don't want to rely on this. Is there any function in C# for determining the currently installed software?

Upvotes: 3

Views: 4412

Answers (3)

Eternal21
Eternal21

Reputation: 4684

Check MySQL installation registry key:

bool IsMySqlInstalled()
{
   return Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MySQL AB") != null;
}

MySQL details on what key is created can be found here, under 'Changes to the Registry' section:

The MySQL Installation Wizard creates one Windows registry key in a typical install situation, located in HKEY_LOCAL_MACHINE\SOFTWARE\MySQL AB.

Upvotes: 1

Jeremy McGee
Jeremy McGee

Reputation: 25210

You can do this through WMI: the class you need is Win32_Product.

It's really easy in Powershell:

Get-WmiObject -Class Win32_Product 

will get the list of installed products, which you can then filter.

In C#, try the System.Management namespace:

    public bool CheckForMySQLServer()
    {
        string query = "SELECT Name FROM Win32_Product WHERE Name LIKE '%MySQL Server%'";

        var searcher = new ManagementObjectSearcher(query);
        var collection = searcher.Get();

        return collection.Count > 0;
    }

Note that this is hideously slow - takes over a minute on my PC - but you can get hold of the version number string if you need (see the GetText() method on the collection items).

Upvotes: 3

Eugen Rieck
Eugen Rieck

Reputation: 65294

Assuming that you also need to access MySQL (a.o.t. just knowing about its presence) you could

  • (Optional) load GAC copy of MySQL connector via reflection in a try-catch block, if that fails
  • load local copy of MySQL connector
  • Try to connect to localhost with bad username/password
  • Check error code. If this is "access denied" you have MySQL

Upvotes: 0

Related Questions