private void AutoStart_CheckedChanged(object sender, EventArgs e)
{
try
{
if (bool_IsStarting) return;
string strName = Application.ExecutablePath;
string strnewName = strName.Substring(strName.LastIndexOf(@"\") + 1);
if (AutoStart.Checked)
{
if (!File.Exists(strName))//指定文件是否存在
return;
Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (Rkey == null)
Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
Rkey.SetValue(strnewName, strName);//修改注册表,使程序开机时自动执行。
}
else
{
Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
Rkey.DeleteValue(strnewName, false);
}
BtnApply.Enabled = true;
}
catch
{
MessageBox.Show("系统管理权限不足,请先退出软件,右键单击以“管理员身份运行”,然后再进行设置");
AutoStart.CheckState = CheckState.Unchecked;
}
}