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?
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 } } }
Use the GetValue method, specifying the path and name) to read a value from registry key. The following example reads the value Name from HKEY_CURRENT_USER\Software\MyApp and displays it in a message box.
Click Start or press the Windows key . In the Start menu, either in the Run box or the Search box, type regedit and press Enter . In Windows 8, you can type regedit on the Start screen and select the regedit option in the search results. In Windows 10, type regedit in the Search box on the taskbar and press Enter .
You can access the Registry via the Registry Editor app into Windows. The view is divided into a list of keys (folders) on the left and values on the right. Navigating it is much like browsing for files using File Explorer. Select a key on the left and you'll see the values that key contains on the right.
Registry values are name/data pairs stored within keys. Registry values are referenced separately from registry keys. Each registry value stored in a registry key has a unique name whose letter case is not significant.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With