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

使程序开机自启

时间:2019-01-28 10:53:32      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:dex   creat   delete   inf   val   info   event   png   系统   

实现效果:

     技术分享图片

知识运用:

  注册表项:[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] 下的键值对

实现代码:

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                string pro = textBox1.Text.Trim();
                string proName = textBox1.Text.Substring(pro.LastIndexOf("\\")+1);  //获取文件名
                RegistryKey rk=Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",true);
                if (rk ==null)                                                      //如果指定项不存在
                    rk = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                rk.SetValue(proName,pro);
                if (MessageBox.Show("设置成功!") == DialogResult.OK)
                    RefreshSystem();                                               //刷新系统
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                try
                {
                    string pro = textBox1.Text.Trim();
                    string proName = textBox1.Text.Substring(pro.LastIndexOf("\\") + 1);  //获取文件名
                    RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                    if (rk == null)                                                      //如果指定项不存在
                        rk = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    rk.DeleteValue(proName);
                    if (MessageBox.Show("设置成功!") == DialogResult.OK)
                        RefreshSystem(); 
                }
                catch{}                                              
            }
        }

  

使程序开机自启

标签:dex   creat   delete   inf   val   info   event   png   系统   

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

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