求修改注册表的简短的代码,并带有注释
例如我要把QQ应用程序写入注册表

解决方案 »

  1.   


    try                                                //可能有异常,放在try块中
                {
                    RegistryKey rsg = null;                    //声明变量
                    rsg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft", true); //true表可修改
                    if (rsg.GetValue("XXX") != null)  //如果值不为空
                    {
                        count = int.Parse(rsg.GetValue("XXX").ToString());
                        rsg.SetValue("XXX", (count + 1).ToString());//创建键值//读取值
                    }
                    else
                    {
                        rsg.SetValue("XXX", (count + 1).ToString());//创建键值                }
                   
                    rsg.Close();                            //关闭
                }
                catch (Exception ex)                        //捕获异常
                {
                    MessageBox.Show("读取写入注册表错误!");
                    Application.Exit();
                    //显示异常信息
                }
      

  2.   


    try                                                //可能有异常,放在try块中
                {
                    RegistryKey rsg = null;                    //声明变量
                    rsg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft", true); //true表可修改
                    if (rsg.GetValue("XXX") != null)  //如果值不为空
                    {
                        count = int.Parse(rsg.GetValue("XXX").ToString());
                        rsg.SetValue("XXX", (count + 1).ToString());//创建键值//读取值
                    }
                    else
                    {
                        rsg.SetValue("XXX", (count + 1).ToString());//创建键值                }
                   
                    rsg.Close();                            //关闭
                }
                catch (Exception ex)                        //捕获异常
                {
                    MessageBox.Show("读取写入注册表错误!");
                    Application.Exit();
                    //显示异常信息
                }