【问题标题】:detect if machine is online or offline using WMI and C#使用 WMI 和 C# 检测机器是在线还是离线
【发布时间】:2011-01-26 21:21:26
【问题描述】:

我用的是vs2008,winxp,局域网内有Win2003服务器。

我想在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

【问题讨论】:

  • 不确定我得到“何时”,但如果机器离线,告诉它重新启动不是有点困难吗?
  • @Hans Passant:我也在考虑这个问题,我相信他想检测机器何时可用,即。 e.已启动。

标签: c# connection wmi monitoring ping


【解决方案1】:

我会选择 .NET System.Net.NetworkInformation.Ping,因为它非常灵活,您可以异步执行它,而且我发现它比 WMI 更直观(我已经使用了两者,并且仅在需要时才使用 WMI来自远程机器的更多信息,而不仅仅是 ping)。但这只是个人意见。

【讨论】:

    【解决方案2】:

    如果机器支持 ICMP 回显请求,您可以使用 Ping 类而不是 WMI。

    【讨论】:

      【解决方案3】:

      不确定这个问题到底是为了什么,但对于它的价值,我有一个测试框架,可以在 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;
                  }
              }
          }
      

      这有点天真,但它有效:)

      【讨论】:

        猜你喜欢
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-03
        • 1970-01-01
        • 1970-01-01
        • 2015-11-15
        相关资源
        最近更新 更多