如何使用C#从注册表中读取(默认)键值 [英] How to read (Default) key value from registry using C#

查看:797
本文介绍了如何使用C#从注册表中读取(默认)键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想从Registry中读取(默认)中的值。这是在HKEY_LOCAL_MACHINE \



我试过像下面这样的不同方式,但它返回NULL。

 RegistryKey key = Registry.LocalMachine; 
key.OpenSubKey( @ \ SOFTWARE \Classes \ ADs \Clsid);

string InstallPath = string .Empty;
InstallPath =( string )key.GetValue( ); ---- Way1
InstallPath =( string )key.GetValue( ); ---- Way2
InstallPath =( string )key.GetValue( (默认) string .Empty); ---- Way3
InstallPath = key.GetValue( null )。ToString (); --- Way4 // 错误无法分配给对象


string InstallPath =( string )key.GetValue( ); ---- Way5



请指导,我怎样才能获得(默认)的价值



提前致谢:)

解决方案

根据 MSDN文档 [ ^ ]你可以通过指定一个空字符串来获得这个值(如上面的方法2),并且根据个人经验,我知道这是有效的。您需要检查您尝试访问的项目是否确实存在,以及您是否具有必要的权限级别来阅读它们。


一点点细分:

 RegistryKey key = Registry.LocalMachine.OpenSubKey( @  SOFTWARE \Classes \ ADs\Clsid); 



这里有两件事:

1)你可以把前两行合二为一。如果没有,你必须将密钥分配给自己,即

 key = key.OpenSubKey(... 



2)删除第一个反斜杠(在SOFTWARE之前) - 否则你会得到一个对象引用未设置为对象的实例错误



现在,您可以使用:

 InstallPath =( string )key.GetValue( null ); 



3)最后,完成后不要忘记关闭钥匙!


< blockquote>所谓的默认值实际上是一个未命名的值,但RegEdit始终将其显示为(默认)。



通过查找空字符串,即,可以检测是否存在指定的,即非空的默认值由 RegistryKey.GetValueNames()返回的数组。



该值可以用<$ c $读取c> RegistryKey.GetValue()或 RegistryKey.GetValue(null)。如果未分配,那么该值将为null,RegEdit将显示(未设置值)。



Alan。


Hi,

I want to read the value in (Default)from Registry. This is under "HKEY_LOCAL_MACHINE\"

I''hv tried like different ways like below ,but its returning NULL.

RegistryKey key = Registry.LocalMachine;
key.OpenSubKey(@"\SOFTWARE\Classes\ADs\Clsid");

string InstallPath = string.Empty;
InstallPath = (string)key.GetValue(".");                    ---- Way1
InstallPath = (string)key.GetValue("");                     ---- Way2
InstallPath = (string)key.GetValue("(Default)",string.Empty);---- Way3
InstallPath = key.GetValue(".",null).ToString();       --- Way4 // error cann't assign to an object


string InstallPath = (string)key.GetValue("");               ---- Way5


please guide, how can i get the value of (Default)

Thanks in advance:)

解决方案

According to the MSDN documentation[^] you can get this value by specifiying an empty string (as in Way 2 above), and from personal experience I know this works. You need to check that the items you are trying to access actually exist, and also that you have the requisite privilege level to read them.


A little breakdown:

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\ADs\Clsid");


2 things here:
1) You can combine the first two lines into one. If not, you must assign the key to itself, i.e.

key = key.OpenSubKey(...


2) Remove the first backslash (before "SOFTWARE") - otherwise you''ll get an "Object reference not set to an instance of an object" error

Now, you can use this:

InstallPath = (string)key.GetValue(null);


3) Finally, don''t forget to close the key when you have finished with it!


The so called default value is really an unnamed value but RegEdit always displays it as "(Default)".

The presence of an assigned, i.e. non null, default value can be detected by looking for an empty string, i.e. "", in the array returned by RegistryKey.GetValueNames().

The value may be read with either RegistryKey.GetValue("") or RegistryKey.GetValue(null). If unassigned then the value will be null and RegEdit will display "(value not set)".

Alan.


这篇关于如何使用C#从注册表中读取(默认)键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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