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 ;}