!!!!启动应用时引用
#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;//禁止关闭状态