C # Set Automatic startup and Shutdown

Source: Internet
Author: User

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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.