VB.NET實現關機和重新啓動

VB.NET實現關機和重新啓動 
━━━━━━━━━━━━━━━━━━━━━━━━━ 
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer 
    Const EWX_FORCE As Short = 4 
    Const EWX_LOGOFF As Short = 0 
    Const EWX_REBOOT As Short = 2 
    Const EWX_SHUTDOWN As Short = 1 
    Dim retval As Integer 
    ' 定義Esc按鍵  
    Const VK_ESCAPE As Short =  & H1Bs 

Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click 
    If Option1.Checked Then 
        ' 註銷當前用戶  
        retval = ExitWindowsEx(EWX_FORCE, 0) 
    ElseIf Option2.Checked Then 
        ' 關閉計算機  
        retval = ExitWindowsEx(EWX_SHUTDOWN, 0) 
    ElseIf Option3.Checked Then 
        ' 重新啓動  
        retval = ExitWindowsEx(EWX_REBOOT, 0) 
    End If 
End Sub 

Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click 
    Me.Close() 
End Sub 

' 按Esc鍵時,結束應用程序  
Private Sub Form1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress 
    Dim KeyAscii As Short = Asc(eventArgs.KeyChar) 
    If KeyAscii = VK_ESCAPE Then 
        Me.Close() 
    End If 
    If KeyAscii = 0 Then 
        eventArgs.Handled = True 
    End If 
End Sub 
    ━━━━━━━━━━━━━━━━━━━━━━━━━ 
     
    本實例通過使用ExitWindowEx()API函數來達到關機和重新啓動的目的。在ExitWindowEx()函數中,參數uFlags指定要進行何種操作。在表86 - 2中列出了參數uFlags的值及其說明。 
    表86 - 2 參數uFlags的值及說明 
    常量名值說明 
    EWX_FORCE 4 
    終止所有進程,包括沒有響應的進程,並註銷Windows 
    EWX_REBOOT 2 
    重新啓動系統 
    EWX_SHUTDOWN 1 
    關閉系統 
    EWX_LOGOFF 0 
    終止所有正在運行的進程,並註銷Windows 
━━━━━━━━━━━━━━━━━━━━━━━━━ 
Vb.net強制關機代碼 

Shell("cmd.exe /c shutdown -s -t 10000") 

system.diagnostic.process.start("shutdown") 
━━━━━━━━━━━━━━━━━━━━━━━━━ 
Shell "cmd.exe /c shutdown -s -t 0" 
若要重啓,把 - s 改爲 - r 
不加 - f ,vbHide也可以省去, - t 後的數字最好不要是0,以保證在關機前可以用代碼 
Shell "cmd.exe /c shutdown -a" 
取消關機 
━━━━━━━━━━━━━━━━━━━━━━━━━ 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章