• C#实现程序的开机启动


    Code
    using Microsoft.Win32;

    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);
                }
            }

  • 相关阅读:
    【生活美好】H5Resume
    【生活美好】youya-vue
    【美好生活】 haha-edu
    【生活美好】Mobile-H5-web-for-vue
    vue模块引用错误【CaseSensitivePathsPlugin】
    v-auth
    npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained
    【每日一练】两个数字长度一致,不一致的前面加0补齐
    ERROR: CAN'T FIND PYTHON EXECUTABLE "PYTHON", YOU CAN SET THE PYTHON ENV VARIABLE.解决办法
    [ERROR] ionic-app-scripts has unexpectedly closed (exit code 1).
  • 原文地址:https://www.cnblogs.com/a1656344531/p/2837897.html
Copyright © 2020-2023  润新知