WMI 實現Windows代理ping

來源:互聯網
上載者:User

使用了笨辦法取了兩次值,嘗試過ManagementOperationObserver的非同步呼叫,結果出現RPC不可用,暫時沒有時間解決這個問題,請高手看到此文給予指點,謝謝。

代碼如下:           

ConnectionOptions options = new ConnectionOptions();
            options.Username = "administrator";
            options.Password = "*********";

            ManagementScope Conn = new ManagementScope(@"\\XXX.XXX.XXX.XXX\root\cimv2", options);
            Conn.Connect();

            ObjectQuery query = new ObjectQuery("select * from win32_PingStatus where Address = 'www.sina.com.cn'");

            ManagementObjectSearcher mos =
                        new ManagementObjectSearcher(Conn, query);

            ManagementObjectCollection moc = mos.Get();

            Thread.Sleep(1000);

            moc = mos.Get();

            string m = "";

            foreach (ManagementObject mo in moc)
            {
                object obj = mo.Properties["StatusCode"].Value;

                if (obj == null)
                {
                    m = "PING 執行失敗。可能是主機未知。";
                }
                else
                {
                    if (obj.ToString().Trim() == "0")
                    {
                        m = "來自 " + mo.Properties["Address"].Value.ToString() + " 的回複, 位元組: " +
                            mo.Properties["BufferSize"].Value.ToString() + ", 時間: " + mo.Properties["ResponseTime"].Value.ToString() +
                            ", TTL: " + mo.Properties["ResponseTimeToLive"].Value.ToString();
                        break;
                    }
                }
            }

            Console.WriteLine(m);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.