验证是否有可用网络连接的最简单方法是什么?
我是 c#/.net 开发的新手,但我已经为我公司的一小部分资产编写了一个股票跟踪应用程序。 我还在 SQL 2000 中设置了它连接的数据库。
目前,当网络连接可用时,它运行得非常好,但我想扩展它,以便在我没有连接时使用。
首先,我需要知道是否有可用的连接。 所以我把它放在一起:
private int availableNetAdapters()
{
int nicCount = 0;
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
nicCount++;
}
}
return nicCount;
}
似乎可以工作,但我必须测试“> 1”,因为无论其他适配器状态如何,始终会检测到“MS TCP 环回接口”。
有没有更好/更简单的方法来检查连接?
G
I'm a bit of newbie to c#/.net development, but I've put together a stock tracking application for a small set of assets in my company. I have also set up the database it connects to in SQL 2000.
It currently works brilliantly when a network connection is available, but I want to expand it for use when I'm away from a connection.
First off I'll need to know if there's a connection available. So I put this together:
private int availableNetAdapters()
{
int nicCount = 0;
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
nicCount++;
}
}
return nicCount;
}
Seems to work, but I have to test for ">1" as something as "MS TCP Loopback interface" is always detected regardless of the other adapter sates.
Is there a better/easier way to check connectivity?
G
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
您还可以使用该类中的事件
NetworkAvailabilityChanged
和NetworkAddressChanged
来监视 IP 地址和网络可用性发生变化。编辑:请注意,此方法会检查计算机上可能存在的所有可用网络接口(无线、局域网等)。 如果其中任何一个已连接,它将返回 true。
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
You can also use the events
NetworkAvailabilityChanged
andNetworkAddressChanged
in that class to monitor IP address and network availability changes.EDIT: Be aware that this method checks all available network interfaces that may be on the computer (wireless, lan, etc.). If anyone of them is connected, it will return true.
还有一些需要记住的事情:
因此,通常最好直接测试对您需要的特定资源的访问。
这些资源是不稳定的; 无论如何,当它们下降时,你必须处理异常。 因此,通常最好只是获取资源,就好像您确定它存在一样,并投入开发时间以确保您的异常处理程序能够很好地处理失败。
Some more things to remember:
Therefore it's generally best to test for access to the specific resource you need directly.
These resources are volatile; you have to handle the exception when they go down anyway. So it's generally best to just go get a resource as if you know for sure it exists and put your development time in to making sure your exception handler does a nice job with the failures.
类
这是您将调用的来自客户端的
然后您将编写方法来捕获上面定义的 PingHost 事件
Here's the class
From the Client you would call
Then you would code up methods to capture the PingHost's events defined above
我们有一个主要在通过无线空中卡连接的警车中运行的应用程序。 我们需要一种方法来确定它们是否已连接。 我们编写了一个非常小的 Ping.cs 类,用于定期 ping 预定的 IP 或计算机名称。 我们解释结果消息并更改系统托盘连接图标的显示。 如果您愿意,我可以向您发送 C# 代码。
We have an application that runs mainly in police cars connected via wireless air cards. We needed a way to determine if they were connected or not. We wrote a very small Ping.cs class that pings a predetermined IP or computer name at a regular intervals. We interpret the result message and change the display of a system tray connection icon. If you want I can send you the C# code.