In C #, use the DOS command to close the currently running program and restart it.

Source: Internet
Author: User

In C #, you can use pocess. Start to call the doscommand to complete many operations, which is very convenient. For example, shut down the current application and restart the computer.

The following describes several common commands:

// Restart the computer command

Private void restartpc ()

{

// Restart the computer to issue the command
Process. Start ("shutdown.exe", "-R ");
// Close all related processes
Process. getcurrentprocess (). Kill ();

}

// Shut down the computer command

Private void shutdownpc ()

{

// Restart the computer to issue the command
Process. Start ("shutdown.exe ");
// Close all related processes
Process. getcurrentprocess (). Kill ();

}

// Disable the computer function in a heavy load to set the countdown.

Public static void shutdownpc (string time)
{
String S = "-s-t" + time;
Process. Start ("shutdown.exe", S );

}

// Log out

Public static void logoff ()
{
Process. Start ("shutdown.exe", "-l ");
}

// Undo to close the computer

Public static void cancelshutdown ()
{
Process. Start ("shutdown.exe", "-");
}

// Open an application, for example, enable the Upgrade Program.

/// <Summary>
/// Start the Upgrade Program
/// </Summary>
Private void startupdatesys ()
{
// Start the Upgrade Program
Process [] vaproc = process. getprocessesbyname ("versionagent ");
If (vaproc. Length = 0)
{
Process proc = new process ();
Proc. startinfo. filename = "versionagent.exe ";
Proc. startinfo. Arguments = "/P1/b38400/fstock/Mr ";
Proc. startinfo. windowstyle = system. Diagnostics. processwindowstyle. hidden;
Proc. Start ();
}
}

/// <Summary> /// set the program to start running /// </Summary> /// <Param name = "started"> whether to start running </param> // /<Param name = "exename"> name of the EXE program to be run (do not extend the name) </param> /// <Param name = "path"> path of the EXE program to be run </param> /// <returns> success returns true, otherwise, false </returns> Public bool runwhenstart (bool started, string exename, string path) {registrykey key = registry. localmachine. opensubkey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true); // open the Registry subitem if (Key = NULL) // If This item does not exist, create this subitem {key = registry. localmachine. createsubkey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run");} If (started = true) {try {key. setvalue (exename, PATH); // set it to the START key. close () ;}catch {return false ;}} else {try {key. deletevalue (exename); // cancel the startup key. close () ;}catch {return false ;}} return true ;}

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.