如何从使用 VB.Net 框架 2.0 或更高版本的编码重新启动 PC
我也喜欢为重启过程设置计时器。
VB.net 框架是否支持上述过程?我需要添加库或 DLL 文件吗
提前致谢。
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:
Using Win32 API.
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
看:
代码: = System.Diagnostics.Process.Start("ShutDown", "/r") 以下是其他选项: C:>shutdown 用法:shutdown [-i | -l | -s | -r | -a] [-f] [-m \computername] [-t xx] [-c "comment"] [-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