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

问题描述 投票:45回答:2

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

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

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

如果用户安装了连接器,我想检查它们的版本,存储为Name =“Version”和Data = x.x.x(下图)

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

怎样才能解决这个问题?

编辑:下面是我目前的代码,我在第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
        }
    }
}
c# registry
2个回答
81
投票

您需要先将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中做很多事情。有时甚至读取值可能是没有管理员权限的可疑操作。


-7
投票

更改:

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"))
© www.soinside.com 2019 - 2024. All rights reserved.