在带有Win2003服务器的局域网中,我使用vs2008,winxp。
我想在winxp中安装一个应用程序,用于检测win2003机器是在线还是脱机,如果脱机,则在启动它时进行检测。
我有这些参考资料,还有更多的参考资料,代码样本和最佳实践吗?
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/
发布于 2011-01-27 05:35:01
我会选择.NET System.Net.NetworkInformation.Ping,因为它非常灵活,你可以异步操作,而且我发现它比WMI更直观(我两者都用过,只有当我需要从远程机器获取比ping更多的信息时才使用WMI )。但这只是我个人的观点。
发布于 2011-01-27 05:33:18
如果机器支持ICMP echo请求,则可以使用Ping类而不是WMI。
发布于 2012-01-19 08:23:17
我不确定这个问题到底是为了什么,但就它的价值而言,我有一个在VM上运行测试并需要重新启动它们的测试框架。在重启计算机后(通过WMI),我等待ping失败,然后ping成功(使用其他人提到的System.Net.NetworkInformation.Ping ),然后我需要等待,直到Windows准备就绪:
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;
}
}
}这有点幼稚,但它是有效的:)
https://stackoverflow.com/questions/4810180
复制相似问题