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

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

相关推荐
吴可可1233 分钟前
C# CAD二次开发中如何退出窗口
c#
影寂ldy6 分钟前
C# WinForm 三种自定义控件 + 完全自定义重绘控件知识点
开发语言·c#
-银雾鸢尾-21 分钟前
C#中的Dictionary相关方法
开发语言·c#
lzhdim1 小时前
C# 中读取CPU、硬盘和内存温度
开发语言·c#
WarPigs2 小时前
.NET项目app.Config笔记
c#·.net
weixin_4280053017 小时前
C#调用 AI学习从0开始-第3阶段RAG向量数据库-文档切分与入库第15天
人工智能·学习·c#·向量数据库·rag·qdrant·文档切分与入库
心平气和量大福大18 小时前
C#-WPF-布局-Grid
c#·wpf·visual studio
TeamDev18 小时前
JxBrowser 9.3.1 版本发布啦!
java·前端·javascript·c#·混合应用·jxbrowser·浏览器控件
格林威21 小时前
工业相机Chunk功能全解析:图像嵌入时间戳、编码器元数据(附堡盟C#代码)
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·工业相机