为了账号安全,请及时绑定邮箱和手机立即绑定

检查C#是否可以使用Internet连接

检查C#是否可以使用Internet连接

C#
哆啦的时光机 2019-10-19 15:04:23
检查编程连接是否可用的最简单方法是什么?编辑:按照建议,我尝试使用以下方法,但始终返回true。[Flags]enum InternetConnectionState : int{    INTERNET_CONNECTION_MODEM = 0x1,    INTERNET_CONNECTION_LAN = 0x2,    INTERNET_CONNECTION_PROXY = 0x4,    INTERNET_RAS_INSTALLED = 0x10,    INTERNET_CONNECTION_OFFLINE = 0x20,    INTERNET_CONNECTION_CONFIGURED = 0x40}class Program{    [DllImport("WININET", CharSet = CharSet.Auto)]    static extern bool InternetGetConnectedState(ref InternetConnectionState lpdwFlags, int dwReserved);static void Main(string[] args){    InternetConnectionState flags = 0;    bool isConnected = InternetGetConnectedState(ref flags, 0);    Console.WriteLine(isConnected);    //Console.WriteLine(flags);    Console.ReadKey();}}其他信息(如果有帮助):我通过共享的wifi网络访问互联网。
查看完整描述

3 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

这是您可以调用的Windows API。它位于wininet.dll中,称为InternetGetConnectedState。


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 ) ;

    }

}


查看完整回答
反对 回复 2019-10-19
  • 3 回答
  • 0 关注
  • 634 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信