detect if machine is online or offline using WMI and C#

╄→гoц情女王★ 提交于 2019-12-02 05:51:27

问题


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


回答1:


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.




回答2:


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




回答3:


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 :)



来源:https://stackoverflow.com/questions/4810180/detect-if-machine-is-online-or-offline-using-wmi-and-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!