首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用WMI和C#检测机器是联机还是脱机

问使用WMI和C#检测机器是联机还是脱机
EN

Stack Overflow用户
提问于 2011-01-27 05:21:26
回答 3查看 2.7K关注 0票数 2

在带有Win2003服务器的局域网中,我使用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/

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

EN

回答 3

Stack Overflow用户

发布于 2011-01-27 05:35:01

我会选择.NET System.Net.NetworkInformation.Ping,因为它非常灵活,你可以异步操作,而且我发现它比WMI更直观(我两者都用过,只有当我需要从远程机器获取比ping更多的信息时才使用WMI )。但这只是我个人的观点。

票数 2
EN

Stack Overflow用户

发布于 2011-01-27 05:33:18

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

票数 1
EN

Stack Overflow用户

发布于 2012-01-19 08:23:17

我不确定这个问题到底是为了什么,但就它的价值而言,我有一个在VM上运行测试并需要重新启动它们的测试框架。在重启计算机后(通过WMI),我等待ping失败,然后ping成功(使用其他人提到的System.Net.NetworkInformation.Ping ),然后我需要等待,直到Windows准备就绪:

代码语言:javascript
复制
    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;
            }
        }
    }

这有点幼稚,但它是有效的:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4810180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档