保存程序配置到注册表里

准备: 
    手动或用代码在注册表 HKEY_LOCAL_MACHINE/SOFTWARE/ 在新建的 "XXX"  项下添加需要的变量,例如添加

名:Text  值:    

名:R       值:255 
名:G       值:255 
名:B       值:255        的字符串值

用以下代码也可实现添加;
        private void buildreg()
        {
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine;
            Microsoft.Win32.RegistryKey softwareXXX = key.CreateSubKey("software//XXX");
            softwareXXX.SetValue("Text""");
            softwareXXX.SetValue("R""255");
            softwareXXX.SetValue("G""255");
            softwareXXX.SetValue("B""255");
        }

读取方法: 
        private void Form1_Activated(object sender, EventArgs e)
        {
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine;
            Microsoft.Win32.RegistryKey softwareXXX = key.OpenSubKey("software//XXX");
            textBox1.Text = softwareXXX.GetValue("Text").ToString();
            int R = int.Parse(softwareXXX.GetValue("R").ToString());
            int G = int.Parse(softwareXXX.GetValue("G").ToString());
            int B = int.Parse(softwareXXX.GetValue("B").ToString());
            textBox1.BackColor = Color.FromArgb(R, G, B);
        } 

保存方法:
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine;
            Microsoft.Win32.RegistryKey softwareXXX = key.CreateSubKey("software//XXX");
            softwareXXX.SetValue("Text", textBox1.Text );
            softwareXXX.SetValue("R", textBox1.BackColor.R.ToString());
            softwareXXX.SetValue("G", textBox1.BackColor.G.ToString());
            softwareXXX.SetValue("B", textBox1.BackColor.B.ToString());
        }
posted @ 2012-03-31 08:54  影子科技  阅读(386)  评论(0编辑  收藏  举报