Most software provides this function to enable automatic startup or shutdown. How to implement it is actually very simple. You only need to set the Registry to run it at startup. to shut it down, call the CMD command: shutdown-s-t, as shown below:
Automatic startup:
/// <Summary>
/// Set the startup operation
/// </Summary>
Public void AutoRun ()
{
RegistryKey runItem =
Registry. CurrentUser. OpenSubKey (@ "Software \ Microsoft \ Windows \ CurrentVersion \ Run", true );
If (runItem = null)
{
Run. SetValue ("exe name", "exe path ");
}
}
/// <Summary>
/// Cancel startup
/// </Summary>
Public void DeleteAutoRun ()
{
RegistryKey runItem =
Registry. CurrentUser. OpenSubKey (@ "Software \ Microsoft \ Windows \ CurrentVersion \ Run", true );
If (runItem! = Null)
{
RunItem. DeleteSubKey ("exe name ");
}
}
/// <Summary>
/// Set the startup operation
/// </Summary>
Public void AutoRun ()
{
RegistryKey runItem =
Registry. CurrentUser. OpenSubKey (@ "Software \ Microsoft \ Windows \ CurrentVersion \ Run", true );
If (runItem = null)
{
Run. SetValue ("exe name", "exe path ");
}
}
/// <Summary>
/// Cancel startup
/// </Summary>
Public void DeleteAutoRun ()
{
RegistryKey runItem =
Registry. CurrentUser. OpenSubKey (@ "Software \ Microsoft \ Windows \ CurrentVersion \ Run", true );
If (runItem! = Null)
{
RunItem. DeleteSubKey ("exe name ");
}
}
Set shutdown: www.2cto.com
Public static string ExecuteCmd (string command)
{
String output = ""; // output string
If (command! = Null &&! Command. Equals (""))
{
Process process = new Process (); // create a Process object
ProcessStartInfo startInfo = new ProcessStartInfo ();
StartInfo. FileName = "cmd.exe"; // set the command to be executed
StartInfo. Arguments = "/C" + command; // "/C" indicates to exit immediately after the command is executed.
StartInfo. UseShellExecute = false; // do not start with the system shell program
StartInfo. RedirectStandardInput = false; // The input is not redirected.
StartInfo. RedirectStandardOutput = true; // redirect output
StartInfo. CreateNoWindow = true; // do not create a window
Process. StartInfo = startInfo;
Process. Start ();
}
}
Public static string ExecuteCmd (string command)
{
String output = ""; // output string
If (command! = Null &&! Command. Equals (""))
{
Process process = new Process (); // create a Process object
ProcessStartInfo startInfo = new ProcessStartInfo ();
StartInfo. FileName = "cmd.exe"; // set the command to be executed
StartInfo. Arguments = "/C" + command; // "/C" indicates to exit immediately after the command is executed.
StartInfo. UseShellExecute = false; // do not start with the system shell program
StartInfo. RedirectStandardInput = false; // The input is not redirected.
StartInfo. RedirectStandardOutput = true; // redirect output
StartInfo. CreateNoWindow = true; // do not create a window
Process. StartInfo = startInfo;
Process. Start ();
}
}
Call:
ExecuteCmd ("shutdown-s-t ");
ExecuteCmd ("shutdown-s-t ");
From Poplar