#region//Auto-run on boot
private void Cb_auto_checkedchanged (object sender, EventArgs e)
{//cb_auto is a checkbox,isautorun is a Boolean variable that controls whether the boot runs
if (cb_auto.checked = = true) Isautorun = true;
else Isautorun = false;
Try
{
AutoRun ();
}
Catch
{ }
}
private void AutoRun ()
{
Gets the program execution path:
string staruppath = Application.executablepath;
Class Micosoft.Win32.RegistryKey. Represents an item-level node in the window registry, which is a registry-mounted form.
RegistryKey Loca = registry.localmachine;
RegistryKey run = Loca. CreateSubKey (@ "SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
Try
{
SetValue: The name of the stored value
if (Isautorun = = False) run. SetValue ("WinForm", false);//Cancel boot Run
else run. SetValue ("WinForm", staruppath);//Set boot run
Loca. Close ();
}
Catch
{}
}
#endregion
Determine if the program has been set to boot automatically, write in Form_Load
RegistryKey Loca_chek = registry.localmachine;
RegistryKey Run_check = Loca_chek. CreateSubKey (@ "SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (Run_check.getvalue ("WinForm"). ToString (). ToLower ()! = "false")
{/respectively/corresponds to the above WinForm and false
Cb_auto.checked = true;
}
Else
{
cb_auto.checked = false;
}
C#.net Form set/Cancel boot automatically run, determine whether the program has been set to boot automatically start (reprint)