惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
雷峰网
雷峰网
博客园_首页
小众软件
小众软件
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
U
Unit 42
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 叶小钗

博客园 - 阿冠

git报错unable to read tree xxxxxx socket tcp 报错误码 103/10053 原因 NuGet 未能为 SSL/TLS 安全通道建立信任关系 NuGet 未能为 SSL/TLS 安全通道建立信任关系 Visual Studio Font Cache 字体缓存路径下的文件太大了 ControlTemplate 中 Bingding 附加属性时需要加入 Path WPF使用第三方的字体(TTF文件) Base class for cloning an object in C# WPF 竖排文字 WPF 将DLL嵌入EXE文件(安装包) WPF 程序自删除(自毁)|卸载程序删除 WPF listbox UI虚拟化 记一次纠结Macbook 重装OS X的系统 指定的元素已经是另一个元素的逻辑子元素。请先将其断开连接。(解决问题总结) 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转换为类型“System.Collections.Generic.IList`1 foreach---集合已修改;可能无法执行枚举操作。 WPF_View中控件使用单例ViewModel 判断s2是否能够被通过s1做循环移位(rotate)得到的字符串是否包含 多列转1列 SqlServer 实现oracle10g的 wmsys.wm_concat()--for xml path('')
关闭Windows 系统当前连接的Wifi以及判断物理\虚拟网卡,有线...
阿冠 · 2018-04-25 · via 博客园 - 阿冠

1.关闭wifi ,调用Api

 [DllImport("Wlanapi.dll", SetLastError = true)]
 public static extern uint WlanDisconnect(IntPtr hClientHandle, ref Guid pInterfaceGuid, IntPtr pReserved);

2.获取当前连接wifi 网卡句柄

The WlanOpenHandle function opens a connection to the server.

就是为了返回 ClientHandle ,传入WlanDisconnect(...) 中的hClientHandle 的参数。

官方解释【A handle for the client to use in this session. This handle is used by other functions throughout the session.】

 [DllImport("Wlanapi.dll")]
 private static extern int WlanOpenHandle(uint dwClientVersion, IntPtr pReserved,  [Out]out uint pdwNegotiatedVersion, out IntPtr ClientHandle);

3.WlanDisconnect(...)中还有一个参数pInterfaceGuid要获取,就是真实网卡(安装了虚拟机要虚拟网卡)的唯一标识符,无线和有线还不一样。

3.1 真实网卡可以通过Win32_NetworkAdapter(可以查询此类的属性,其中有【GUID】) 获取,因为真实网卡的 PNPDeviceID  都是【PCI】开头

SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus=2  AND PNPDeviceID LIKE 'PCI%'");

以上 获取了已连接的真实网卡,笔记本一般有无线和有线网卡,如果无线和有线都连接,获取了两条记录,NetConnectionStatus 条件是连接状态 【2】:已连接,【7】:未连接。

3.2通过Win32_NetworkAdapter我们可以获取 真实网卡的唯一标识符 GUID. 区别有线还是无线,我们可以通过注册表判断注册表路径:HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\【GUID】\Connection

路径下的键值 MediaSubType,如果Value=2,表示无线网卡。

 4. 最终形成

 public static bool Disconnect()
        {
            IntPtr _clientHandle = IntPtr.Zero;
            uint negotiatedVersion;
            int code = WlanOpenHandle(1, IntPtr.Zero, out negotiatedVersion, out _clientHandle);
            NetworkInterface[] _interface = NetworkInterface.GetAllNetworkInterfaces();
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(
            @"SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus=2  AND PNPDeviceID LIKE 'PCI%'");
            string GUID = "";
            foreach (ManagementObject mo in searcher.Get())
            {
                GUID = mo["GUID"].ToString().Trim();
                Console.WriteLine(GUID);
                var registryKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\" + GUID + "\\Connection", false);
                if (registryKey != null)
                {
                    var mediaSubTypevalue = registryKey.GetValue("MediaSubType");
                    if (mediaSubTypevalue != null)
                    {
                        int num;
                        int.TryParse(mediaSubTypevalue.ToString(), out num);
                        if (num == 2)
                        {
                            Guid netIfaceGuid = new Guid(GUID);
                            var ERROR_SUCCESS = WlanDisconnect(_clientHandle, ref netIfaceGuid, IntPtr.Zero);
                            if (ERROR_SUCCESS == 0)
                            {
                                return true;
                            }
                            WlanCloseHandle(_clientHandle, IntPtr.Zero);//不确定
                        }
                    }
                }
            }
            return false;
        }

View Code

5.后记

我用的是NativeWifi 类库,进行打开wifi的,NativeWifi 也是封装了windows 的Wlanapi.dll 的方法。但是没有关闭连接wifi方法,所以只能自己写。

 文章参考:

有线网卡与无线网卡、物理网卡与虚拟网卡的区分