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

后端 未结 3 2032
抹茶落季
抹茶落季 2021-01-21 22:24

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 wh

3条回答
  •  感情败类
    2021-01-21 23:23

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

提交回复
热议问题