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

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

相关推荐
aini_lovee2 小时前
C# 快递单打印系统(万能套打系统)
开发语言·c#
白菜上路2 小时前
C# Serilog.AspNetCore基本使用
c#·serilog
小白不白1112 小时前
C# WinForm 与 VP 二次开发
开发语言·c#
SunnyDays10113 小时前
如何使用 C# 自动调整 Excel 行高和列宽
开发语言·c#·excel
itgather4 小时前
OfficeExcel — Word / Excel DLL 验证台功能介绍
c#·word·excel
云中小生4 小时前
Scrutor:.NET 依赖注入自动化的优雅实现
c#·.net
郝亚军4 小时前
Visual Studio 2022项目中的.sln是什么?
c++·c#·visual studio
jghhh014 小时前
C# 图片水印工具(支持9个位置)
数据库·microsoft·c#
咸鱼翻身小阿橙5 小时前
C# WinForms 控件学习项目
开发语言·学习·c#
JaydenAI5 小时前
[MAF预定义Agent中间件-03]FunctionInvocationDelegatingAgent:将AOP引入函数调用
ai·c#·agent·aop·maf