C# 判断电脑是否联网

项目中连接webAPI需要判断是否联网,故找到这个方法,不需要引用任何dll,代码复制一下,直接使用。wininet.dll是系统自带的

cs 复制代码
   public  void Initial()
        {
            try
            {               
                if (IsNetworkConnected)
                {
                    SvMaster.Log.WriteInfo("网络连接成功");
                }
                else
                {
                    SvMaster.Log.WriteError("网络连接失败,请检查网络!");
                }
            }
            catch (Exception ex)
            {
                SvMaster.Log.WriteError(ex);
            }
        }      
        [DllImport("wininet.dll", EntryPoint = "InternetGetConnectedState")]
        public extern static bool InternetGetConnectedState(out int conState, int reader);

        public static bool IsNetworkConnected
        {
            get
            {
                return InternetGetConnectedState(out int n, 0);
            }
        }

说明:该方法亲测可用,记录。

相关推荐
唐青枫3 小时前
对象到底住在哪里?C#.NET 托管堆从分配、GC 到内存排查
c#·.net
fogota5 小时前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
灸木6 小时前
C# 多线程与异步
c#
鱼听禅10 小时前
C# 学习笔记-使用 类型化客户端搭建HTTP客户端服务
学习·http·c#·工厂方法模式
geovindu12 小时前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
淡海水12 小时前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
fogota13 小时前
C# 如何调用系统图标
c#·.net
△曉風殘月〆13 小时前
.NET是什么?为什么要选择.NET?
c#·.net·.net core·clr
fogota13 小时前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota13 小时前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf