Turn off the WiFi currently connected to the Windows system and determine the physical \ Virtual network card, wired \ Wireless network card

Source: Internet
Author: User

1. Turn off WiFi and call API

[DllImport ("Wlanapi.dll"true)]  Public Static extern UINT ref Guid Pinterfaceguid, IntPtr preserved);

2. Get the current connected WiFi adapter handle

The wlanopenhandle function opens a connection to the server.

Just to return Clienthandle, pass in the parameters of Hclienthandle in Wlandisconnect (...).

The official explanation is "A handle for the client" and the use of this session. This handle was used by and functions throughout the session. "

[DllImport ("Wlanapi.dll"privatestaticextern  int wlanopenhandle (uint dwclientversion, IntPtr preserved,  [out]outuint  out IntPtr clienthandle);

3.WlanDisconnect (...) There is also a parameter pinterfaceguid to get, is the real network card (virtual machines installed virtual network card) Unique identifier, wireless and wired is not the same.

3.1 Real network cards can be obtained through Win32_NetworkAdapter (you can query the properties of this class, which have a "GUID"), because the Pnpdeviceid of the real network card is the beginning of "PCI"

SELECT * from Win32_NetworkAdapter WHERE netconnectionstatus=2  'pci%' ") ;

The above obtains the connected real network card, the notebook generally has the wireless and the wired network card, if the wireless and the wired are connected, obtains two records, the netconnectionstatus condition is the connection status "2": Connected, "7": Not connected.

3.2 By Win32_NetworkAdapter We can get the unique identifier GUID of the real NIC. The difference between wired and wireless, we can determine the registry path through the registry: hklm\system\currentcontrolset\control\network\{4d36e972-e325-11ce-bfc1-08002be10318} \ "GUID" \connection

The key value under the path Mediasubtype, if value=2, represents the wireless network card.

4. Final formation

  Public Static BOOLDisconnect () {IntPtr _clienthandle=IntPtr.Zero; UINTnegotiatedversion; intCode = Wlanopenhandle (1, IntPtr.Zero, outNegotiatedversion, out_clienthandle); Networkinterface[] _interface=networkinterface.getallnetworkinterfaces (); ManagementObjectSearcher Searcher=NewManagementObjectSearcher (@"SELECT * from Win32_NetworkAdapter WHERE netconnectionstatus=2 and Pnpdeviceid like ' pci% '"); stringGUID =""; foreach(ManagementObject moinchSearcher. Get ()) {GUID= mo["GUID"]. ToString ().                Trim ();                Console.WriteLine (GUID); varRegistryKey = Registry.LocalMachine.OpenSubKey (@"system\currentcontrolset\control\network\{4d36e972-e325-11ce-bfc1-08002be10318}\"+ GUID +"\\Connection",false); if(RegistryKey! =NULL)                {                    varMediasubtypevalue = RegistryKey.GetValue ("Mediasubtype"); if(Mediasubtypevalue! =NULL)                    {                        intnum; int. TryParse (Mediasubtypevalue.tostring (), outnum); if(num = =2) {Guid Netifaceguid=NewGUID (GUID); varError_success = Wlandisconnect (_clienthandle,refNetifaceguid, IntPtr.Zero); if(Error_success = =0)                            {                                return true; } wlanclosehandle (_clienthandle, IntPtr.Zero);//not sure                        }                    }                }            }            return false; }
View Code

5. PostScript

I use the Nativewifi class library to open WiFi, Nativewifi is also a way to encapsulate the Wlanapi.dll of Windows. But there is no way to turn off the WiFi connection, so you can write it yourself.

Article reference:

The distinction between wired network card and wireless network card, physical network card and virtual network card

Turn off the WiFi currently connected to the Windows system and determine the physical \ Virtual network card, wired \ Wireless network connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.