// LaunchOnBoot是int类型的变量,0:开机不自启;1:开机自启
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//如果是管理员,则写入到注册表
if (LaunchOnBoot == 1) // 开机自启
{
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("JcShutdown", path);
rk2.Close();
rk.Close();
}
else // 取消开机自启
{
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.DeleteValue("JcShutdown", false);
rk2.Close();
rk.Close();
}
}
else
{
// 如果不是管理员,则写到当前用户的注册表中去
if (LaunchOnBoot == 1) // 开机自启
{
string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("JcShutdown", path);
rk2.Close();
rk.Close();
}
else // 取消开机自启
{
string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.DeleteValue("JcShutdown", false);
rk2.Close();
rk.Close();
}
}