在下面的代码中Registry.LocalMachine.CreateSubKey("Software\\" + Settings.Default.ApplicationName))后,打开注册表后,并没有创建相应的项.但奇怪的是str居然能取到值,为桌面路径,
问题1.为什么会这样
问题2.如何解决
string str = string.Empty;using (RegistryKey key = Registry.LocalMachine.CreateSubKey("Software\\" + Settings.Default.ApplicationName))
{
  str = key.GetValue("Path", string.Empty).ToString();
}                   
             

解决方案 »

  1.   

    试了,没有发现问题,str的值为""; 
      

  2.   

    可能楼上是在管理员权限下运行是对的,可在windows标准用户下就会出上面的问题
      

  3.   

    如果你用Vista,先在注册表中查一下这里(<User SID>_Classes要换成适合的当前用户):
    HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software如果在这里能找到你创建的键,那么你的问题出于Vista的注册表虚拟化:Registry Virtualization解决方法同样参考链接文章(更好的办法是避免写到LocalMachine\SOFTWARE项下)。
      

  4.   

    设置以值看看,再打开。
     RegistryKey rsg = null;
    string str="";
                    rsg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\A",true);
                    if (rsg.GetValue("PATH") != null)                 {
                        str= rsg.GetValue("path").ToString();
                    }
                    else
                        str = "该键不存在!";
                    rsg.Close();