如何阅读的注册表项,C#值 [英] How to read value of a registry key c#

查看:129
本文介绍了如何阅读的注册表项,C#值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序启动时,我想看看,如果用户安装了软件的特定版本,具体MySQL连接器,全部采用C#。在注册表中,MySQL的包含一个版本的条目。所以,我要完成的就是这个。

我的应用程序启动。某处在启动code,我需要做以下的事情秩序。检查用户是否安装了MySQL连接器,它位于...


  

HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Wow6432Node \\ MySQL AB公司\\ MySQL的连接器/网络


如果用户已经安装了连接器,我想检查他们有什么版本,存储为名称=版本和Data = X.X.X(下图)

现在,如果用户已经安装了一个特定的版本,那么我会执行其它code,这是我可以拿。

什么是要去这个的最佳方式?

编辑:下面是code我现在有,我第19行得到一个错误(这是注释)。我的错误说错误CS1001:标识符预期我不是能够找出这意味着什么。任何帮助吗?

 使用系统;
使用的Microsoft.Win32;
使用System.Data这;公共类regTest
{
    公共静态无效的主要()
    {
        尝试
        {
            关键的RegistryKey = Registry.LocalMachine.OpenSubKey(SOFTWARE \\\\ \\\\ Wow6432Node MySQL AB公司\\\\的MySQL Connector \\\\网);
            如果(关键!= NULL)
            {
                对象o = key.GetValue(版本);
                如果(O!= NULL)
                {
                    版本版本=新版本(邻为String); //作为,因为它是REG_SZ ...否则的ToString()可能是安全的(R)
                    打破版本=新版本(6.7.4);
                    如果(version.Equals。(碎))//这是其中误差是发生
                    {
                        数据集的数据集= ConfigurationManager.GetSection(System.Data这)作为ystem.Data.DataSet;                        数据视图VI = dataSet.Tables [0] .DefaultView;
                        vi.Sort =姓名;
                        如果(vi.Find(MySQL的)== -1)
                        {
                            dataSet.Tables [0] .Rows.Add(MySQL的
                                MySql.Data.MySqlClient
                                MySql.Data.MySqlClient
                                ,
                                typeof运算(MySql.Data.MySqlClient.MySqlClientFactory).AssemblyQualifiedName);
                        }                    }                }
            }
        }        赶上(异常前)//只是为了演示......它总是最好来处理特定的异常
        {
             //作出适当的反应
        }
    }
}


解决方案

您需要首先添加使用的Microsoft.Win32; 您code页

然后就可以开始使用注册类:

 尝试
{
    使用(的RegistryKey键= Registry.LocalMachine.OpenSubKey(SOFTWARE \\\\ \\\\ Wow6432Node MySQL AB公司\\\\的MySQL Connector \\\\网))
    {
        如果(关键!= NULL)
        {
            对象o = key.GetValue(版本);
            如果(O!= NULL)
            {
                版本版本=新版本(邻为String); //作为,因为它是REG_SZ ...否则的ToString()可能是安全的(R)
                //做你喜欢的版本
            }
        }
    }
}
赶上(异常前)//只是为了演示......它总是最好来处理特定的异常
{
    //作出适当的反应
}

请注意:除非你有管理员权限,你是不太可能能够做很多的 LOCAL_MACHINE 。有时甚至读值可以是没有管理员权限嫌疑人的操作。

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

解决方案

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.

这篇关于如何阅读的注册表项,C#值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆