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

本文关键字:注册表 访问 应用程序 net | 更新日期: 2023-09-27 18:21:05

我有一个类,在构造函数中,我试图从注册表中读取一个键的值。当我尝试将事件日志名称设置为从注册表返回的值(在本例中为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() 

请在下面找到为注册表访问而编写的代码。


object keyVal =
            Registry.GetValue(SEL_ENTITY_REGISTRY_KEY,
            "EventLogName", string.Empty);
        this._logName = (keyVal == null) ? null : keyVal.ToString();

但是,当我在另一个.net应用程序中读取相同的注册表值时,该应用程序会返回注册表值。新应用程序也具有与上述相同的代码。

所以看起来不像是权限问题。有人能帮我解决问题吗。感谢您提前提供的帮助。

谢谢&当做

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

在获取值之前,必须指定从何处获取值。

GetValue的参数是键名、值的名称和默认值。

在你的代码中(我可以看到),你实际上在哪里打开了一个注册表路径。

相反,要获得您想要的指定值:

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