how to restart PC in C#

  • Thread starter Thread starter Guest
  • Start date Start date
[DllImport("advapi32.dll", EntryPoint="InitiateSystemShutdown")]
public static extern int InitiateSystemShutdownA(string lpMachineName,
string lpMessage, int dwTimeout, int bForceAppsClosed, int
bRebootAfterShutdown);
public void RestartPC(object sender, System.EventArgs e)
{
int restartcode=InitiateSystemShutdownA("\\127.0.0.1", "Restart...", 10,
1, 1);
MessageBox.Show("OK"+restartcode);
}

Am i right?

but the parameter restartcode is 0,and my computer still run.
 
You can also use WMI to do this, this will allow you to also reboot remote
machines, as well as force reboots so that it will not hault to get a user
key press to end tasks.

[DllImport("advapi32.dll", EntryPoint="InitiateSystemShutdown")]
public static extern int InitiateSystemShutdownA(string lpMachineName,
string lpMessage, int dwTimeout, int bForceAppsClosed, int
bRebootAfterShutdown);
public void RestartPC(object sender, System.EventArgs e)
{
int restartcode=InitiateSystemShutdownA("\\127.0.0.1", "Restart...", 10,
1, 1);
MessageBox.Show("OK"+restartcode);
}

Am i right?

but the parameter restartcode is 0,and my computer still run.


W.G. Ryan eMVP said:
You cna use the API
http://custom.programming-in.net/articles/art9-1.asp?f=InitiateSystemShutdown
Or you can freestyle and just loop through each active process on your
computer and shut it down -

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
 
Back
Top