简体   繁体   English

如何确定网络接口是否连接到Internet(即计算机是否在线)

[英]How to determine if network interface is connected to the Internet (i. e. the computer is online)

I need a way to determine internet availability programmatically. 我需要一种以编程方式确定互联网可用性的方法。 At now i use Ping to constantly ping some internet site. 现在我用Ping来不断ping一些网站。

But it seems Windows 7 though determines internet availability in some other way. 但似乎Windows 7虽然以其他方式确定了互联网可用性。 If computer is online there is earth icon on the network interface icon in the System Tray. 如果计算机处于联机状态,则系统托盘中的网络接口图标上会显示地球图标。

The question is: is there any standard Win32 way to check online status, win event or something, and if so, how to use it from C#? 问题是:是否有任何标准的Win32方式来检查在线状态,赢取事件或其他什么,如果有,如何从C#中使用它?

I believe something like this would work although your question appears to be a duplicate: 虽然你的问题似乎是重复的,但我相信这样的事情会奏效:

using System;
using System.Runtime;
using System.Runtime.InteropServices;

public class InternetCS
{
    //Creating the extern function...
    [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState( out int Description, int ReservedValue );

    //Creating a function that uses the API function...
    public static bool IsConnectedToInternet( )
    {
        int Desc ;
        return InternetGetConnectedState( out Desc, 0 ) ;
    }
}

forund here: 在这里:

check whether Internet connection is available with C# 检查C#是否可以使用Internet连接

'Connected to the Internet' doesn't have any actual meaning except in the case of a modem. 除了调制解调器之外,“连接到互联网”没有任何实际意义。 The beat way to test whether any resource is available is to use it. 测试任何资源是否可用的节拍方法是使用它。 You have to cope with failures at that point anyway, no need to code everything twice. 无论如何,你必须应对失败,不需要对所有内容进行两次编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定哪个网络适配器连接到Internet - How to determine which network adapter is connected to the internet 将一维数组的索引转换为二维数组,即行和列 - Converting index of one dimensional array into two dimensional array i. e. row and column 我怎么知道我的计算机没有连接到网络? - How do I know if my computer's not connected to a network? 如何检查网络上的计算机是否在线? - How can I check if a computer on my network is online? 如何运行联网计算机的进程? - How to run process of internet connected computer? 如何确定我的计算机正在使用哪个网络适配器? - How do I determine which network adapter my computer is using? 如何确定WP7中是否有人连接到互联网? - How to determine if someone is connected to the internet in WP7? 如何在Android中检查移动网络是否连接到互联网 - How to check if mobile network is connected to the internet or not in android 如何确定网络路径是否可用(在线或离线)? - How to determine if a network path is available or not (online or offline)? 如何确定我是否在Windows Phone 8.1(通用应用程序)中连接到WiFi或移动网络? - How can I determine whether I am connected to WiFi or a mobile network in Windows Phone 8.1 (Universal app)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM