无法从注册表项获取值 [英] Can not get value from registry key

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

问题描述

当我调试以下内容时,我在RegKey附近得到的值数为0.

所以我无法从注册表获得价值



< b>我尝试了什么:



 使用的Microsoft.Win32; 
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 DEPWeb
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
chk();

}

private void chk()
{
string sRegistryKey = Software\\NCS\\DoorEntProg;
RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(sRegistryKey);
if (RegKey == null
{ // 仅限第一次
RegKey = Registry.CurrentUser.CreateSubKey(sRegistryKey);
}
else
{
string filename = ( string )RegKey.GetValue( FileName);
}
// 尝试
// {
// 使用(RegistryKey key = Registry.CurrentUser.OpenSubKey(Software \\NCS \\DoorEntProg))
/ / {
// if(key!= null)
// {
// string filename =(string)key.GetValue(FileName);
< span class =code-comment> //
}
// }
// }
// catch(Exception ex)//仅用于演示......总是最好处理特定的例外
// {
// //做出适当反应
// }
}
}
}

解决方案

< blockquote>请参阅以下链接,了解从注册表项获取价值的完美解决方案。希望它适合您..



 http: //   stackoverflow.com/questions/18232972/how-to-read-value-of-a-registry-键-C-尖锐 

< pre lang = C#>你需要首先添加 使用 Microsoft.Win32;到您的代码页。

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

尝试
{
使用(RegistryKey key = Registry.LocalMachine.OpenSubKey(& quot; Software \\Wow6432Node\\MySQL AB \\ MySQL Connector \\\ \\ net& quot;))
{
if (key!= null
{
Object o = key.GetValue(& quot; Version& quot;);
if (o!= null
{
版本号= new 版本(o as String ) ; // & quot; as& quot;因为它的REG_SZ ...否则ToString()可能是安全的(r)
// 根据版本做你喜欢的事情
}
}
}
}
catch (例外情况) // 仅供演示......它总是最好处理特定例外
{
// 做出适当反应
} < / pre >


when i debug the following i am getting value count as 0 near RegKey.
So I cant get value from Registry

What I have tried:

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DEPWeb
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            chk();
            
        }

        private void chk()
        {
            string sRegistryKey = "Software\\NCS\\DoorEntProg";
            RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(sRegistryKey);
            if (RegKey == null)
            {       // First time only
                RegKey = Registry.CurrentUser.CreateSubKey(sRegistryKey);
            }
            else
            {
                string filename = (string)RegKey.GetValue("FileName");
            }
            //try
            //{
            //    using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\NCS\\DoorEntProg"))
            //    {
            //        if (key != null)
            //        {
            //            string filename = (string)key.GetValue("FileName");
            //        }
            //    }
            //}
            //catch (Exception ex)  //just for demonstration...it's always best to handle specific exceptions
            //{
            //    //react appropriately
            //}
        }
    }
}

解决方案

Please see the following link for perfect solution for getting value from registry key..Hope it will work for you..

http://stackoverflow.com/questions/18232972/how-to-read-value-of-a-registry-key-c-sharp

<pre lang="C#">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(&quot;Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net&quot;))
    {
        if (key != null)
        {
            Object o = key.GetValue(&quot;Version&quot;);
            if (o != null)
            {
                Version version = new Version(o as String);  //&quot;as&quot; because it&#39;s REG_SZ...otherwise ToString() might be safe(r)
                //do what you like with version
            }
        }
    }
}
catch (Exception ex)  //just for demonstration...it&#39;s always best to handle specific exceptions
{
    //react appropriately
}</pre>


这篇关于无法从注册表项获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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