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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18232972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:35:23  来源:igfitidea点击:

How to read value of a registry key c#

c#registry

提问by Scalahansolo

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.

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

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

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

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

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)

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

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?

编辑:下面是我目前拥有的代码,我在第 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
        }
    }
}

采纳答案by DonBoitnott

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

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

Then you can begin to use the Registryclasses:

然后你就可以开始使用这些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
}

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.

注意:除非您拥有管理员访问权限,否则您不太可能在LOCAL_MACHINE. 有时,即使读取值也可能是没有管理员权限的可疑操作。

回答by Manuel

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