2012-05-10 67 views
1

如何用VB.Net framework 2.0或更高版本重新編碼電腦用VB.NET編碼重新啓動電腦

我也喜歡爲重啓過程設置定時器。

上面的程序是否支持VB.net框架?是否需要添加庫或DLL文件

在此先感謝。

回答

2

有這麼多的方式我們可以通過編碼來關閉或重新啓動窗口。但我喜歡現在最簡單的方式和最短的方式如下。 這沒什麼,使用「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 
1

參見:

代碼:= System.Diagnostics.Process.Start( 「關斷」, 「/ R」)以下是 另一選項:C:> shutdown用法:shutdown [-i | -l | -s | -r | -a] [-f] [-m \計算機名] [-t XX] [-c 「C omment」] [-d起來: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

編號:Programmatically restart computer from .NET

有,你可以用它來關閉/通過C#代碼重新啓動您的計算機不同的方法。以下是其中一些:

  1. 使用Win32 API。

  2. 使用「Shutdown」命令。即內部發射的shutdown.exe/s的

使用 「關機」 命令:

代碼段

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

+0

+1雖然這是一個VB.net的問題,你已經發布了c# – MarkJ

0

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 
相關問題