WMI 实现Windows代理ping
使用了笨办法取了两次值,尝试过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);