简体   繁体   English

从.net应用程序访问注册表

[英]Accessing registry from .net application

I have a class in which in the constructor i am trying to read a value from registry for a key. 我有一类在构造函数中我试图从注册表中读取一个键的值。 The value is returned back as null and throws the following exception when I try to set the event log name to the value returned from registry (which is turning out to be null in this case): 当我尝试将事件日志名称设置为注册表返回的值时,该值返回为null并引发以下异常(在这种情况下,结果为null):

System.ArgumentNullException: Value cannot be null.
Parameter name: value
at System.Diagnostics.EventLog.SetLogName(String currentMachineName, String value)
at System.Diagnostics.EventLog.set_Log(String value)
at Net.GVN.EventLogging.EventLogger.set_Log(String value)
at Net.BusinessEntities.Sel.get_SelEntityEventLogger()
at Net.BusinessEntities.Sel..ctor() 

Please find below the code which is written for registry access. 请在下面找到编写用于注册表访问的代码。


object keyVal =
            Registry.GetValue(SEL_ENTITY_REGISTRY_KEY,
            "EventLogName", string.Empty);

        this._logName = (keyVal == null) ? null : keyVal.ToString();

But when I write another .net application in which I just read the same registry value, this application returns back the registry values. 但是,当我编写另一个.net应用程序时,只要我在其中读取相同的注册表值,该应用程序就会返回注册表值。 The new application also has the same code as mentioned above. 新应用程序还具有与上述相同的代码。

So does not look like permission issue. 因此看起来不像权限问题。 Can someone help me regarding what the issue might be please. 有人可以帮助我解决这个问题吗。 Thanks for your help in advance. 感谢您的帮助。

Thanks & Regards... 感谢和问候...

Before you get a value you must specify where you are getting it from. 在获取值之前,必须指定从何处获取值。

GetValue's parameters are a key name, the name of the value, and the default value. GetValue的参数是键名,值的名称和默认值。

No where in your code(That I can see.) are you actually opening a registry path. 在您的代码中(我可以看到。)您实际上没有打开注册表路径。

Instead to get the specified value that you want: 而是获取所需的指定值:

//make a reference to what part of the registry you want. In this case I chose Current User
RegistryKey myRootKey = Registry.CurrentUser;

//Open the specified subkey below the root key
RegistryKey selectedSubKey = myRootKey.OpenSubKey("FirstSubKey\\Child1\\Child2");

//Now you can use selectedSubKey to have access to all values WITHIN Child2
object keyval = selectedSubKey.GetValue("myValueName");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM