使用WMI和C#检测机器是在线还是离线

我在使用Win2003服务器的LAN网络中使用vs2008,winxp。

我想在winxp中安装一个应用程序来检测win2003机器是在线还是离线,如果在启动它时是否脱机。

我有这个参考,任何更多的参考,代码示例和最佳实践?

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

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

The popular C# Ping Utitility

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

我会选择.NET System.Net.NetworkInformation.Ping ,因为它非常灵活,你有可能异步进行,我发现它比WMI更直观(我已经使用过两者并且只在我需要时使用WMI)从远程机器获取更多信息而不仅仅是ping)。 但这只是个人意见。

如果计算机遵守ICMP echo请求,则可以使用Ping类而不是WMI。

不确定问题的确切含义,但是对于它的价值,我有一个测试框架,可以在VM上运行测试并需要重新启动它们。 重启盒子后(通过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; } } } 

这有点天真,但它的工作原理:)