89

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 } } } 
1

3 Answers 3

146

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have updated my code to reflect what you provided me, but I ran into a problem as shown above, any help?
Take out the extra '.'
One should use 'RegistryKey' within using blocks. They implement IDisposable.
When I try this I get RegistryKey could not be found (I'm using Microsoft.Win32 but still no type found...)
9

@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 } 

Comments

-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")) 

4 Comments

why would you paste the same thing with no improvement?
better to remove this one, because doesn't contribute with OP question
@zalimgandhera Well, the "same thing" would have been to put an @ in front of the string. So it's not the same thing. It's more broken.
Remove this section, This answer is of no help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.