Copy codeThe Code is as follows: private void btnSetOk_Click (object sender, EventArgs e)
{
RegCompStartRun (true, txtFullPath. Text. Trim ());
}
Private void btnCancel_Click (object sender, EventArgs e)
{
RegCompStartRun (false, txtFullPath. Text. Trim ());
}
Private void RegCompStartRun (bool cmd, string argPath)
{
String starupPath = argPath;
If (string. IsNullOrEmpty (argPath ))
{
// Obtain the full path of the current executable program
StarupPath = Application. ExecutablePath;
}
// Indicates the entry-level node in the Window registry. It reads the HKEY_LOCAL_MACHINE key of the Windows registry.
Microsoft. Win32.RegistryKey loca = Microsoft. Win32.Registry. LocalMachine;
Microsoft. Win32.RegistryKey run = loca. CreateSubKey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run ");
Try
{
// SetValue: name of the stored value
If (cmd)
{
Run. SetValue ("AutoStartupTestWinFormApp", starupPath); // Add to registration. The parameter is the name of the registered node (random)
LblDisplay. Text = "set successfully! ";
}
Else
{
Run. DeleteValue ("AutoStartupTestWinFormApp", false); // Delete the registered Node
LblDisplay. Text = "the settings have been canceled! ";
}
Loca. Close ();
}
Catch (Exception ee)
{
MessageBox. Show (ee. Message. ToString (), "prompt", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
}
Note:
If you rename the exe file after setting, log out of the file, restart the file, and log on again. The set boot item does not start,
If you change to the original name, log off or restart again. After logon, the set exe file is automatically started.