2011-01-26 60 views
2

我在Win2003服務器的局域網中使用vs2008,winxp。使用WMI和C檢測機器是在線還是離線#

我想要一個安裝在winxp中的應用程序來檢測win2003機器是否聯機或脫機,以及在啓動時是否脫機。

我有這個引用,更多的參考資料,代碼示例和最佳實踐?

http://danielvl.blogspot.com/2004/06/how-to-ping-in-c-using.html

http://snipplr.com/view/2157/ping-using-wmi-pingstatus/

http://dotnoted.wordpress.com/2005/01/15/the-popular-c-ping-utitility/

http://www.visualbasicscript.com/Ping-WMI-amp-NonWMI-Versions-Functions-amp-Simple-Connectivity-Monitor-m42535.aspx

+0

不知道我得到「何時」,但如果一臺機器脫機不會有點難以告訴它重新啓動? – 2011-01-26 21:28:36

回答

1

如果機器兌現ICMP迴應請求,您可以使用Ping類而不是WMI。

2

我會去.NET System.Net.NetworkInformation.Ping,因爲它非常靈活,您可以異步執行此操作,而且我發現它比WMI更直觀(我已經使用了這兩種操作,並且只有在需要獲取更多內容時才使用WMI來自遠程機器的信息,而不僅僅是ping)。但這只是個人意見。

0

不確定問題到底是什麼,但是對於它的價值,我有一個運行虛擬機測試並需要重新啓動它們的測試框架。 (通過WMI)重啓盒子後,我等待一個ping失敗,那麼一個ping成功(使用System.Net.NetworkInformation.Ping如其他人所說),那麼我需要等到Windows準備就緒:

private const int RpcServerUnavailable = unchecked((int)0x800706BA); 

    private const int RpcCallCancelled = unchecked((int)0x80010002); 

    public bool WindowsUp(string hostName) 
    { 
     string adsiPath = string.Format(@"\\{0}\root\cimv2", hostName); 
     ManagementScope scope = new ManagementScope(adsiPath); 
     ManagementPath osPath = new ManagementPath("Win32_OperatingSystem"); 
     ManagementClass os = new ManagementClass(scope, osPath, null); 

     ManagementObjectCollection instances = null; 
     try 
     { 
      instances = os.GetInstances(); 
      return true; 
     } 
     catch (COMException exception) 
     { 
      if (exception.ErrorCode == RpcServerUnavailable || exception.ErrorCode == RpcCallCancelled) 
      { 
       return false; 
      } 
      throw; 
     } 
     finally 
     { 
      if (instances != null) 
      { 
       instances.Dispose(); 
       instances = null; 
      } 
     } 
    } 

這是一個有點天真,但它的工作:)