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

发布于 2024-10-15 01:19:12 字数 905 浏览 6 评论 0原文

我使用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-utility/

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

I use vs2008, winxp, in LAN network with Win2003 servers.

I want a application installed in winxp for detect if win2003 machines is online or offline , and if offline when boot it.

I have this references, any more references, code samples and best practices ??

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放我走吧 2024-10-22 01:19:12

我会选择 .NET System.Net.NetworkInformation.Ping,因为它非常灵活,您可以异步执行它,而且我发现它比 WMI 更直观(我已经使用过和仅当我需要从远程计算机获取更多信息而不仅仅是 ping 时才使用 WMI)。但这只是个人意见。

I would go for the .NET System.Net.NetworkInformation.Ping, because it is quite flexible, you have the possibility of doing it asynchronously and I find it more intuitive than WMI (I have used both and use WMI only if I need to get more info from the remote machine than just the ping). But this is just a personal opinion.

世界如花海般美丽 2024-10-22 01:19:12

如果计算机接受 ICMP 回显请求,则可以使用 Ping 类而不是 WMI。

If the machines honor ICMP echo requests, you can use the Ping class instead of WMI.

行至春深 2024-10-22 01:19:12

不确定问题到底是什么,但就其价值而言,我有一个测试框架,可以在虚拟机上运行测试并需要重新启动它们。重新启动盒子(通过 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;
            }
        }
    }

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

Not sure exactly what the question is for, but for what it's worth, I have a test framework that runs tests on VMs and needs to reboot them. After rebooting the box (via WMI) I wait for a ping fail, then a ping success (using System.Net.NetworkInformation.Ping as mentioned by others) then I need to wait until Windows is ready:

    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;
            }
        }
    }

It's a little naive, but it works :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文