82

At start up of my application I am trying to see if the user has a specific version of a software installed, specifically the MySQL connector, all using c#. In the registry, the MySQL contains a version entry. So what I am trying to accomplish is this.

My app starts up. Somewhere in the start up code I need to do the following things in order. Check to see if the user has the MySQL connector installed, which is located at...

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MySQL AB\MySQL Connector/Net

If the user has the connector installed, I wanted to check what version they have, which is stored as Name = "Version" and Data = x.x.x (Picture below)

Now if the user has a specific version installed, then I will execute other code, which is where I can take from.

What would be the best way of going about this?

enter image description here

EDIT: Below is the code I currently have and I am getting an error on line 19 (It is commented). My error says "error CS1001: Identifier Expected" I wasnt able to figure out what that means. Any help?

using System;
using Microsoft.Win32;
using System.Data;

public class regTest
{
    public static void Main()
    {
        try
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net");
            if (key != null)
            {
                Object o = key.GetValue("Version");
                if (o != null)
                {
                    Version version = new Version(o as String);  //"as" because it's REG_SZ...otherwise ToString() might be safe(r)
                    Version broken = new Version("6.7.4");
                    if (version.Equals.(broken)) //This is where the error is occuring
                    {
                        DataSet dataSet = ConfigurationManager.GetSection("system.data") as ystem.Data.DataSet;

                        DataView vi = dataSet.Tables[0].DefaultView;
                        vi.Sort = "Name";
                        if (vi.Find("MySql") == -1)
                        {
                            dataSet.Tables[0].Rows.Add("MySql"
                                , "MySql.Data.MySqlClient"
                                , "MySql.Data.MySqlClient"
                                ,
                                typeof(MySql.Data.MySqlClient.MySqlClientFactory).AssemblyQualifiedName);
                        }

                    }

                }
            }
        }

        catch (Exception ex)  //just for demonstration...it's always best to handle specific exceptions
        {
             //react appropriately
        }
    }
}
Irshad
  • 3,071
  • 5
  • 30
  • 51
Scalahansolo
  • 2,615
  • 6
  • 26
  • 43

3 Answers3

139

You need to first add using Microsoft.Win32; to your code page.

Then you can begin to use the Registry classes:

try
{
    using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net"))
    {
        if (key != null)
        {
            Object o = key.GetValue("Version");
            if (o != null)
            {
                Version version = new Version(o as String);  //"as" because it's REG_SZ...otherwise ToString() might be safe(r)
                //do what you like with version
            }
        }
    }
}
catch (Exception ex)  //just for demonstration...it's always best to handle specific exceptions
{
    //react appropriately
}

BEWARE: unless you have administrator access, you are unlikely to be able to do much in LOCAL_MACHINE. Sometimes even reading values can be a suspect operation without admin rights.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
6

@DonBoitnott have a good code, but require admin rights. I use this (only need Read Rights)

 try
 {
      var subKey = "Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net";
      using (var key = Registry.LocalMachine.OpenSubKey(subKey, false)) // False is important!
      {
           var s = key?.GetValue("Version") as string;
           if (!string.IsNullOrWhiteSpace(s))
           {
                var version = new Version(s);
           }
      }
 }
 catch (Exception ex)  //just for demonstration...it's always best to handle specific exceptions
 {
      //react appropriately
 }
Marcello Miorelli
  • 3,368
  • 4
  • 44
  • 67
Victor Sanchez
  • 583
  • 9
  • 28
-20

Change:

using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net"))

To:

 using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\Wow6432Node\MySQL AB\MySQL Connector\Net"))
Jeff Woodard
  • 647
  • 8
  • 15
Manuel
  • 9