C#实现程序开机启动

如何用c#实现开机启动?其实用c#实现程序的开机启动大致有两种方法,就是写入注册表或者采用服务程序,最近一直研究着用C#来操作注册表,下面介绍的方法便是用注册表来实现程序随开机启动(高手就不用看了,嘿嘿...)。
1)引入命名空间 using Microsoft.Win32;

//打开注册表子项
RegistryKey key = Registry.LocalMachine.OpenSubKe("SOFTWARE//Microsoft//Windows//CurrentVersion//Run",true);

key.SetValue(程序的名称, 程序的路径);//设置为开机启动
为了安全性,还应该判断设置的文件是否存在以及该子项是否存在,所有完整代码如下:

private void btnShowOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "可执行文件(*.exe)|*.exe";
            if (open.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = open.FileName;
            }
        }

        private bool runWhenStart(bool started,string exeName, string path)
        {          
            RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);//打开注册表子项
            if (key == null)//如果该项不存在的话,则创建该子项
            {
                key = Registry.LocalMachine.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
            }
            if (started == true)
            {
                try
                {                   
                    key.SetValue(exeName, path);//设置为开机启动
                    key.Close();
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                try
                {                   
                    key.DeleteValue(exeName);//取消开机启动
                    key.Close();
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            if (txtPath.Text == "")//检查是否选择了文件
            {
                MessageBox.Show("请选择要随计算机一起启动的程序路径!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPath.Focus();
                return;
            }           
            string path = txtPath.Text.Trim();
            string exeName = path.Substring(path.LastIndexOf("//") + 1);
            if (!File.Exists(path))//检查该文件是否存在
            {
                MessageBox.Show("文件不存在!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPath.Text = "";
                txtPath.Focus();
                return;
            }
            if (runWhenStart(true,exeName, path))
            {
                MessageBox.Show("设置成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("设置失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (txtPath.Text == "")//检查是否选择了文件
            {
                MessageBox.Show("请选择要撤销随计算机一起启动的程序路径!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPath.Focus();
                return;
            }
            string path = txtPath.Text.Trim();
            string exeName = path.Substring(path.LastIndexOf("//") + 1);
            if (!File.Exists(path))//检查该文件是否存在
            {
                MessageBox.Show("文件不存在!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPath.Text = "";
                txtPath.Focus();
                return;
            }
            if (runWhenStart(false, exeName, path))
            {
                MessageBox.Show("撤销成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("撤销失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值