0

我有一个类,在构造函数中我试图从注册表中读取一个键的值。当我尝试将事件日志名称设置为从注册表返回的值(在这种情况下结果为 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 应用程序时,我只是在其中读取了相同的注册表值,该应用程序会返回注册表值。新应用程序也具有与上述相同的代码。

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

感谢和问候...

4

1 回答 1

0

在你得到一个值之前,你必须指定你从哪里得到它。

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");
于 2013-07-11T17:54:58.480 回答