使用了笨辦法取了兩次值,嘗試過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);