注册 登录
编程论坛 C# 论坛

C#检测注册表项是否存在时出错

smooth 发布于 2015-06-17 12:03, 2286 次点击
C#代码检测注册表项是否存在时:
private bool IsRegeditItemExist()
{
    string[] subkeyNames;
    RegistryKey hkcu = Registry.CurrentUser;
    RegistryKey software = hkcu.OpenSubKey("SOFTWARE");
    subkeyNames = software.GetSubKeyNames();
    foreach (string keyName in subkeyNames)
    {
        if (keyName == "TKeysoft") //判断子项的名称
         {
              hkcu.Close();
              return true;
              RegistryKey key = Registry.CurrentUser;
              RegistryKey FileSafeSingle = key.OpenSubKey("Software\\TKeysoft\\FileGee\\FileSafeSingle", true); //该项必须已存在
              FileSafeSingle.SetValue("ImportantCount", "10066328", RegistryValueKind.DWord);
              key.Close();
          }
     }
     hkcu.Close();
     return false;
}
出现以下三个错误:
错误    1    应输入 }   
错误    2    应输入类型、命名空间定义或文件尾
错误    3    应输入类型、命名空间定义或文件尾

如果我把第一行代码private bool IsRegeditItemExist()和第12行代码return true及第20行代码return false注释掉,则代码就正常。不知道是什么问题呢?谢谢!
7 回复
#2
Maick2015-06-17 15:21
请了解return 的作用,....
 return true;
请问这些还会执行吗??
              RegistryKey key = Registry.CurrentUser;
              RegistryKey FileSafeSingle = key.OpenSubKey("Software\\TKeysoft\\FileGee\\FileSafeSingle", true); //该项必须已存在
              FileSafeSingle.SetValue("ImportantCount", "10066328", RegistryValueKind.DWord);
              key.Close();
#3
smooth2015-06-17 16:20
以下是引用Maick在2015-6-17 15:21:41的发言:

请了解return 的作用,....
 return true;
请问这些还会执行吗??
              RegistryKey key = Registry.CurrentUser;
              RegistryKey FileSafeSingle = key.OpenSubKey("Software\\TKeysoft\\FileGee\\FileSafeSingle", true); //该项必须已存在
              FileSafeSingle.SetValue("ImportantCount", "10066328", RegistryValueKind.DWord);
              key.Close();

谢谢回复。我刚学,只能依葫芦画瓢,在摸索中。
我的理解,return true是告知程序,判断注册表项是“存在”的这个种结果。
我把return true放到key.Close()之后,或者把return true注销掉,还是一样的错误提示。

[ 本帖最后由 smooth 于 2015-6-17 16:23 编辑 ]
#4
Maick2015-06-17 16:35
提示你少了}你就检查一下{}是否都对称啊
#5
smooth2015-06-17 17:08
回复 4楼 Maick
程序代码:

private bool IsRegeditItemExist()
{
    string[] subkeyNames;
    RegistryKey hkml = Registry.LocalMachine;
    RegistryKey software = hkml.OpenSubKey("SOFTWARE");//RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
    subkeyNames = software.GetSubKeyNames(); //取得该项下所有子项的名称的序列,并传递给预定的数组中
    foreach (string keyName in subkeyNames)   //遍历整个数组
    {
        if (keyName == "test") //判断子项的名称
        {
            hkml.Close();
            return true;
         }
    }
    hkml.Close();
    return false;
}

这个是我从百度里找到的代码,我们的编程论坛里也有一模一样的代码,我没有做过任何改动,就是提示:
错误    1    应输入 }   
错误    2    应输入类型、命名空间定义或文件尾
错误    3    应输入类型、命名空间定义或文件尾
只有本站会员才能查看附件,请 登录


[ 本帖最后由 smooth 于 2015-6-17 17:11 编辑 ]
#6
Maick2015-06-18 13:21
方法嵌套了方法,,..看到没???
你把IsRegeditItemExist()这个方法放外面然后在From1_Load方法调用..还有我觉得你的方法名要改一下不,跟类名一样(成构造函数了),
#7
Maick2015-06-18 13:23
不是构造函数..我看错..后半句当我没说过
#8
smooth2015-06-18 18:59
以下是引用Maick在2015-6-18 13:23:08的发言:

不是构造函数..我看错..后半句当我没说过

谢谢你,我还是没弄出个结果来。
1