日期:2014-05-18  浏览次数:20915 次

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

------解决方案--------------------
C# code

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();
                //显示异常信息
            }