基于.net类库的注册表自定义项读写操作

 

1)在VS2013新建C#windows form程序,添加引用using Microsoft.Win32;

2)在窗体上添加ComboBox控件,添加2buttontext属性分别为“保存列表到注册表”、“结束”。

3)添加Form1的全局变量String[] strProjectNoList=newString[5];

(4)添加注册表保存函数SaveProjectNoToRegistry()如下。具体使用RegistryKey类操作。

        publicvoid SaveProjectNoToRegistry()

        {

            String strPNTotal =strProjectNoList.ToString();

            RegistryKey hklm =Registry.LocalMachine;

            RegistryKey hksoftware =hklm.OpenSubKey("software",true);

            RegistryKey hkCMHICheckList =hksoftware.CreateSubKey("CMHICheckList12");

            hkCMHICheckList.SetValue("ProjectNo0",strProjectNoList[0]);

            hkCMHICheckList.SetValue("ProjectNo1",strProjectNoList[1]);

            hkCMHICheckList.SetValue("ProjectNo2",strProjectNoList[2]);

            hkCMHICheckList.SetValue("ProjectNo3",strProjectNoList[3]);

            hkCMHICheckList.SetValue("ProjectNo4",strProjectNoList[4]);

 

            hkCMHICheckList.Close();

            hksoftware.Close();

            hklm.Close();

        }

5)添加注册表读取函数ReadProjectNoFromRegistry()如下。读取时必须判断是否为null,第一次用时为null

        publicvoidReadProjectNoFromRegistry()

        {

            //从注册表读取最近5个项目,放入strProjectNoList列表。

            RegistryKey hklm =Registry.LocalMachine;

            RegistryKey hksoftware =hklm.OpenSubKey("software");

            RegistryKey hkCMHICheckList =hksoftware.OpenSubKey("CMHICheckList12",false);

 

            if (hkCMHICheckList !=null)

            {

                //如果读不到,则返回缺省值“”。

                strProjectNoList[0] = (String)hkCMHICheckList.GetValue("ProjectNo0","");

                strProjectNoList[1] = (String)hkCMHICheckList.GetValue("ProjectNo1","");

                strProjectNoList[2] = (String)hkCMHICheckList.GetValue("ProjectNo2","");

                strProjectNoList[3] = (String)hkCMHICheckList.GetValue("ProjectNo3","");

                strProjectNoList[4] = (String)hkCMHICheckList.GetValue("ProjectNo4","");

                hkCMHICheckList.Close();

            }

            hksoftware.Close();

            hklm.Close();

        }

(6)添加comboBox1的更新函数ComboBoxItemsRefresh()如下:

        publicvoidComboBoxItemsRefresh()

        {

            ReadProjectNoFromRegistry();

            comboBox1.Items.Clear();

           comboBox1.Items.Add(strProjectNoList[0]);

           comboBox1.Items.Add(strProjectNoList[1]);

           comboBox1.Items.Add(strProjectNoList[2]);

            comboBox1.Items.Add(strProjectNoList[3]);

           comboBox1.Items.Add(strProjectNoList[4]);

            comboBox1.Text =strProjectNoList[0];//选中和显示最新的那个。

        }

7)添加Form1_Load()函数初始化和加载有关内容:

       privatevoid Form1_Load(object sender, EventArgs e)

        {

            strProjectNoList[0] ="";

            strProjectNoList[1] ="";

            strProjectNoList[2] ="";

            strProjectNoList[3] ="";

            strProjectNoList[4] ="";

            ComboBoxItemsRefresh();

        }

(8)添加button1、button2的处理函数:

       privatevoid button2_Click(object sender, EventArgs e)

        {

            if (comboBox1.Text !="" &&comboBox1.Text != strProjectNoList[0] && comboBox1.Text !=strProjectNoList[1] && comboBox1.Text != strProjectNoList[2] &&comboBox1.Text != strProjectNoList[3] && comboBox1.Text !=strProjectNoList[4])

            {

                strProjectNoList[4] =strProjectNoList[3];

                strProjectNoList[3] =strProjectNoList[2];

                strProjectNoList[2] = strProjectNoList[1];

                strProjectNoList[1] =strProjectNoList[0];

                strProjectNoList[0] =comboBox1.Text;

                SaveProjectNoToRegistry();

                ComboBoxItemsRefresh();

            }

        }

 

        privatevoid button1_Click(object sender, EventArgs e)

        {

            this.Close();

        }

(9)运行后,保存和读取后界面如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值