用VB.NET编译重启PC

时间:2012-05-10 04:47:41

标签: vb.net dll shutdown reboot

如何使用VB.Net framework 2.0或以上版本重新启动PC

我也想设置重启过程的计时器。

VB.net框架是否支持上述过程?我需要添加库或DLL文件

提前致谢。

5 个答案:

答案 0 :(得分:3)

参考:Programmatically restart computer from .NET

您可以使用不同的方法通过C#代码关闭/重新启动计算机。以下是其中一些:

  1. 使用Win32 API。

  2. 使用“关机”命令。即内部触发shutdown.exe / s

  3. 使用“关机”命令:

    代码段

    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
    

    在命令提示符下执行"shutdown/?"以了解有关该命令的更多信息。

    参考:Rebooting a Windows Box Programmatically
    另一种方法是Win32 API的ExitWindowsEx API函数

    我们需要做的第一件事是引用InteropServices名称空间,以便我们可以访问Windows API函数。为此,请在表单代码的开头添加一个新的using指令。

    using System.Runtime.InteropServices;

    将以下声明放在表单的类代码块

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

    然后调用此方法..

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

    更多参考文献:
    Shutdown, Restart, or Log Off your computer using VB.Net
    Simplest way to Programmatically ShutDown or Restart your System using C# or VB.NET

答案 1 :(得分:2)

我们可以通过编码关闭或重新启动Windows。但我喜欢现在最简单的方式和最短的方式如下。     没什么,使用“Shell”推荐。

' 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

答案 2 :(得分:1)

请参阅:

  

代码:= System.Diagnostics.Process.Start(“ShutDown”,“/ r”)这里有   其他选项:C:> shutdown用法: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)

答案 3 :(得分:0)

框架中没有任何内容,但可以使用p / Invoke来调用ExitWindowsEx

See the definition at pinvoke.net

Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Int32, ByVal dwReserved As Int32) As Int32

答案 4 :(得分:0)

此?

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