繁体   English   中英

如何读取注册表项的值c#

[英]How to read value of a registry key c#

在我的应用程序启动时,我试图查看用户是否安装了特定版本的软件,特别是 MySQL 连接器,全部使用 c#。 在注册表中,MySQL 包含一个版本条目。 所以我想要完成的是这个。

我的应用程序启动。 在启动代码的某个地方,我需要按顺序执行以下操作。 检查用户是否安装了 MySQL 连接器,该连接器位于...

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

如果用户安装了连接器,我想检查他们拥有的版本,存储为 Name = "Version" 和 Data = xxx(下图)

现在,如果用户安装了特定版本,那么我将执行其他代码,这是我可以从中获取的。

解决这个问题的最佳方法是什么?

在此处输入图像描述

编辑:下面是我目前拥有的代码,我在第 19 行遇到错误(已注释)。 我的错误是“ error CS1001: Identifier Expected ”我无法弄清楚这意味着什么。 有什么帮助吗?

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

您需要先using Microsoft.Win32;添加using Microsoft.Win32; 到您的代码页。

然后您就可以开始使用Registry类了:

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
}

请注意:除非您拥有管理员权限,否则您不太可能在LOCAL_MACHINE做很多事情。 有时甚至读取值可能是没有管理员权限的可疑操作。

@DonBoitnott 有一个很好的代码,但需要管理员权限。 我用这个(只需要阅读权限)

 try
 {
      using (var key = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net", 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
 }

更改:

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

至:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM