使程序开机自启

实现效果:

     

知识运用:

  注册表项:[HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun] 下的键值对

实现代码:

        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(@"SOFTWAREMicrosoftWindowsCurrentVersionRun",true);
                if (rk ==null)                                                      //如果指定项不存在
                    rk = Registry.LocalMachine.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
                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(@"SOFTWAREMicrosoftWindowsCurrentVersionRun", true);
                    if (rk == null)                                                      //如果指定项不存在
                        rk = Registry.LocalMachine.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
                    rk.DeleteValue(proName);
                    if (MessageBox.Show("设置成功!") == DialogResult.OK)
                        RefreshSystem(); 
                }
                catch{}                                              
            }
        }

  

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