码迷,mamicode.com
首页 > 其他好文 > 详细

开机启动项管理

时间:2019-01-24 16:27:55      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:class   load   info   foreach   效果   registry   run   local   view   

实现效果:

  技术分享图片

知识运用:

  RegistryKey类的GetValueNames

  public string[] GetValueName ()  //检索包含与此项关联的所有值名称的字符串数组

  GetValue方法

  public Object GetValue (string name)  //检索与指定名称关联的值

  DeleteValue方法

  public void DeleteValue (string name)    //从注册表中删除指定值

实现代码:

        string[] Macinfo;
        string[] Curinfo;
        private void getMacinfo()
        {
            RegistryKey rk;
            RegistryKey rk2;
            string[] arr=new string[2];
            rk = Registry.LocalMachine;
            rk2 = rk.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",true);
            Macinfo = rk2.GetValueNames();
            foreach (string s in Macinfo)
            {
                arr[0] = s;
                arr[1]=rk2.GetValue(s).ToString();
                ListViewItem lvi = new ListViewItem(arr);
                listView1.Items.Add(lvi);
            }
        }

        private void getCurinfo()
        {
            RegistryKey rk;
            RegistryKey rk2;
            string[] arr = new string[2];
            rk = Registry.CurrentUser;
            rk2 = rk.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            Curinfo = rk2.GetValueNames();
            foreach (string s in Curinfo)
            {
                arr[0] = s;
                arr[1] = rk2.GetValue(s).ToString();
                ListViewItem lvi = new ListViewItem(arr);
                listView1.Items.Add(lvi);
            }
        }
        private bool IsMacinfo(string s)
        {
            foreach (string str in Macinfo)
            {
                if (s == str)
                    return true;
            }
            return false;
        }
        private bool IsCurinfo(string s)
        {
            foreach (string str in Curinfo)
            {
                if (s == str)
                    return true;
            }
            return false;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            getMacinfo();
            getCurinfo();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.CheckedItems.Count > 0)   //如果有选中
            {
                foreach (ListViewItem i in listView1.CheckedItems)
                {
                    if (IsMacinfo(i.Text))          //判断存于
                    {
                        RegistryKey rk;
                        RegistryKey rk2;
                        rk = Registry.LocalMachine;
                        rk2 = rk.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                        rk2.DeleteValue(i.Text); 
                    }
                    if(IsCurinfo(i.Text))    
                    {
                        RegistryKey rk;
                        RegistryKey rk2;
                        rk = Registry.CurrentUser;
                        rk2 = rk.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                        rk2.DeleteValue(i.Text);    //执行删除
                    }
                }
                listView1.Items.Clear();        //清空ListView控件
                getCurinfo();                   //调用刷新
                getMacinfo();                   //调用刷新
            }
        }

  

开机启动项管理

标签:class   load   info   foreach   效果   registry   run   local   view   

原文地址:https://www.cnblogs.com/feiyucha/p/10314775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!