2009-12-16 86 views
0

我想获取值并将值设置为注册表。注册表GetValue函数

当我试图访问不在注册表中的路径时,我正在阅读。

但是,当我使用Registry.SetValue(keyName, "", 0);设置路径时,所有工作正常,我可以从中获取不存在的值。

任何想法,为什么我不能使用我的public int GetComponent(string RegKey)功能在修女现有的路径?

using System; 
using System.Collections.Generic; 
using System.Text; 
using Microsoft.Win32; 


namespace LP_Misc 
{ 
    public class LP_Registery 
    { 
     private const string userRoot = "HKEY_CURRENT_USER"; 
     private const string subkey = @"Software\PCBMatrix\LPWizard"; 
     private string keyName ; 

     public LP_Registery(string folderName) 
     { 
      keyName = userRoot + "\\" + subkey + "\\" + folderName; 
      //Registry.SetValue(keyName, "", 0); 

     } 
     public int GetComponent(string RegKey) 
     { 
      return (int)Registry.GetValue(keyName, RegKey, 0); 

     } 
     public void SetComponent(string RegKey, int RegVal) 
     { 
      Registry.SetValue(keyName, RegKey, RegVal, RegistryValueKind.DWord); 
     } 



    } 
} 

如果可能我应该怎么做,这将是可能的。

谢谢。

回答

1

围绕着GetValue与try - catch块并享受编码。

例外和条件的GetValue

抛出:SecurityException: 的用户没有从注册表键读取所需的权限。

IOException异常:那 包含指定值的RegistryKey已 标记为删除。

ArgumentException:keyName不是 以有效的注册表根开头。


的SetValue方法:

设置上 指定的注册表项指定的名称/值对。 如果指定的密钥 不存在,则创建 。