简体   繁体   中英

reboot the PC from coding with VB.NET

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.

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. ie internally firing shutdown.exe /s

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

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

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) 

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

This?

Private Sub {BTNNAME}_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles {BTNNAME}.Click
  System.Diagnostics.Process.Start("shutdown", "-r -t 00")
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM