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

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

相关推荐
PfCoder3 小时前
C#中定时器之System.Timers.Timer
c#·.net·visual studio·winform
人工智能AI技术10 小时前
【C#程序员入门AI】本地大模型落地:用Ollama+C#在本地运行Llama 3/Phi-3,无需云端
人工智能·c#
MyBFuture14 小时前
C#数组详解:一维二维与交错数组
开发语言·windows·c#·visual studio·vision pro
有来技术15 小时前
ASP.NET Core 权限管理系统(RBAC)设计与实现|vue3-element-admin .NET 后端
vue.js·后端·c#·asp.net·.net
张人玉16 小时前
C#WinFrom中show和ShowDialog的区别
开发语言·microsoft·c#
m0_7482331716 小时前
C#:微软的现代编程利器
开发语言·microsoft·c#
Traced back17 小时前
SQL Server数据自动清理系统最终版(C# WinForms完整源码)
数据库·c#·.net
人工智能AI技术17 小时前
【C#程序员入门AI】Microsoft Extensions for AI (MEAI):统一LLM调用接口,告别厂商绑定
人工智能·c#
William_cl19 小时前
C# ASP.NET路由系统全解析:传统路由 vs 属性路由,避坑 + 实战一网打尽
开发语言·c#·asp.net