winform中设置软件开机自启和防关闭

!!!!启动应用时引用

#region 开机自启
/// <summary>  
/// 修改程序在注册表中的键值  
/// </summary>  
/// <param name="isAuto">true:开机启动,false:不开机自启</param> 
private void AutoStart(bool isAuto = true, bool showinfo = true)
{
    try
    {
        if (isAuto == true)
        {
            RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
            RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
             添加到 当前登陆用户的 注册表启动项
            //RegistryKey RKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            //RKey.SetValue("AppName", @"C:\AppName.exe");

            // 添加到 所有用户的 注册表启动项
            RegistryKey RKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            RKey.SetValue("AppName", @"C:\AppName.exe");
            R_run.SetValue("应用名称", Application.ExecutablePath);
            R_run.Close();
            R_local.Close();
        }
        else
        {
            RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
            RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            R_run.DeleteValue("应用名称", false);
            R_run.Close();
            R_local.Close();
        }
    }
    catch (Exception)
    {
        if (showinfo)
            MessageBox.Show("您需要管理员权限修改", "提示");
    }
}

#endregion

防关闭:

protected override void WndProc(ref Message m)
{
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_CLOSE = 0xF060;
    if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE && close)
    {
        MessageBox.Show("此程序不允许关闭","Warnning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
        return;
    }
       
    base.WndProc(ref m);
}
private bool close = true;//禁止关闭状态

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值