reboot the PC from coding with VB.NET

21,909

Solution 1

There are so many way that we can shutdown or reboot the windows by coding. But I like present very easiest way and shortest way as follow. It’s nothing, using “Shell” commend.

' Example of rebooting PC:
' Reboot computer after timeout of 5
Shell ("Shutdown -r -t 5")  

' Switches:
'   -l  Log off profile
'   -s  Shut down computer
'   -r  Restart computer
'   -f  Force applications to close
'   -t  Set a timeout for shutdown
'   -m \\computer name (Shutdown remote computer)
'   -i  Show the Shutdown GUI

Solution 2

Ref: Programmatically restart computer from .NET

There are different methods that you can use to shutdown/restart your computer through C# code. Following are some of them:

  1. Using Win32 API.

  2. Using "Shutdown" command. i.e. internally firing shutdown.exe /s

Using "Shutdown" command:

Code Snippet

System.Diagnostics.Process.Start("ShutDown", "/r")
// If immediately then use 
// System.Diagnostics.Process.Start("ShutDown", "/r /t 00") 



/s = shutdown
/r = restart
/t = timed shutdown

Do "shutdown/?" at command prompt to know more about the command.

Ref: Rebooting a Windows Box Programmatically
The Another approach is Win32 API's ExitWindowsEx API Function

The first thing that we need to do is to reference the InteropServices namespace so that we can access the Windows API functions. To do this, add a new using directive at the start of the code for the form.

using System.Runtime.InteropServices;

put the following declaration appears within the form's class code block

[DllImport("user32.dll", SetLastError = true)]
static extern int ExitWindowsEx(uint uFlags, uint dwReason);   

then call the this method..

    BOOL b = ExitWindowsEx( EWX_REBOOT|EWX_FORCE,
    SHTDN_REASON_MINOR_MAINTENANCE |
    SHTDN_REASON_FLAG_PLANNED);

More References:
Shutdown, Restart, or Log Off your computer using VB.Net
Simplest way to Programmatically ShutDown or Restart your System using C# or VB.NET

Solution 3

See:

Code: = System.Diagnostics.Process.Start("ShutDown", "/r") Here are the other options: C:>shutdown Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \computername] [-t xx] [-c "c omment"] [-d up:xx:yy]

    No args                 Display this message (same as -?)
    -i                      Display GUI interface, must be the first option
    -l                      Log off (cannot be used with -m option)
    -s                      Shutdown the computer
    -r                      Shutdown and restart the computer
    -a                      Abort a system shutdown
    -m \\computername       Remote computer to shutdown/restart/abort
    -t xx                   Set timeout for shutdown to xx seconds
    -c "comment"            Shutdown comment (maximum of 127 characters)
    -f                      Forces running applications to close without war ning
    -d [u][p]:xx:yy         The reason code for the shutdown
                            u is the user code
                            p is a planned shutdown code
                            xx is the major reason code (positive integer le ss than 256)
                            yy is the minor reason code (positive integer le ss than 65536)

Solution 4

There isn't anything directly in the framework, but can use p/Invoke to call ExitWindowsEx.

See the definition at pinvoke.net:

Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Int32, ByVal dwReserved As Int32) As Int32
Share:
21,909
John Wa Ha
Author by

John Wa Ha

Updated on July 11, 2022

Comments

  • John Wa Ha
    John Wa Ha almost 2 years

    how to reboot the PC from coding with VB.Net framework 2.0 or above

    i also like to set timer for rebooting process.

    does VB.net framework support for above process? do i need to add library or DLL file

    Thanks in advance.