This article summarizes how C# implements the enablement and disabling of local networks. Share it for your reference, as follows:
1) Use
Using Add Reference, importing it into the project will generate 3 references, mainly using NETCONLib.
In the project, use NETCONLib;
Here is the implementation code:
NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = ; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if ( == tagNETCON_MEDIATYPE.NCM_LAN) { (); //Disable the network(); //Enable network} }
2) Use
It is a Windows shell related application program interface dynamic link library file, used to open web pages and files.
Use Add Reference to import into the project.
In the project, use Shell32;
Here is the implementation code:
const string discVerb = "Disable(&B)"; const string connVerb = "Enable(&A)"; Shell sh = new (); Folder folder; Folder fd; folder = (3); foreach (FolderItem myItem in ()) { if ( == "Network Connection") { fd = (Folder); //Disable the networkforeach (FolderItem fi in ()) { foreach (FolderItemVerb Fib in ()) { if ( == discVerb) { (); break; } } (3000); foreach (FolderItemVerb Fib in ()) { //Enable networkif ( == connVerb) { (); break; } } } } }
3) Use
It is a popular installer that supports related files
You cannot import the first two into the project through Add Reference like the previous two, you can only use DllImport
There are many codes, post the main code
[DllImport("")] public static extern IntPtr SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator, IntPtr hwndParent, UInt32 Flags); [DllImport("")] public static extern IntPtr SetupDiGetClassDevs(UInt32 ClassGuid, String e, IntPtr hwndParent, UInt32 Flags); [DllImport("")] static extern Boolean SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, UInt32 MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData); ………… uint NewNetStatus = 0; if (newStatus) NewNetStatus = DICS_ENABLE; else NewNetStatus = DICS_DISABLE; IntPtr NewDeviceInfoSet; SP_DEVINFO_DATA spData = new SP_DEVINFO_DATA(); = (uint)(spData); UInt32 RequiredSize = 0; byte[] st1 = new byte[1024]; uint Data = 0; NewDeviceInfoSet = SetupDiGetClassDevs(0, "PCI", , DIGCF_PRESENT | DIGCF_ALLCLASSES); bool bFound = false; for (uint i = 0; SetupDiEnumDeviceInfo(NewDeviceInfoSet, i, ref spData); i++) { while (!SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, ref spData, SPDRP_HARDWAREID, ref Data, st1, 1024, ref RequiredSize)) { } string str = (st1); ; char[] a ={ '/0' }; string[] strSPlit = (a, ); string HardId = @"PCI/VEN_10EC&DEV_8029&SUBSYS_00000000"; for (uint j = 0; j < ; j++) { if (strSPlit[j] == HardId) { bFound = true; break; } } if (bFound) break; } SP_PROPCHANGE_PARAMS spPropChangeParam = new SP_PROPCHANGE_PARAMS(); = DICS_FLAG_GLOBAL; = NewNetStatus; = (UInt32)(); = DIF_PROPERTYCHANGE; SetupDiSetClassInstallParams(NewDeviceInfoSet, ref spData, ref , (spPropChangeParam)); SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, NewDeviceInfoSet, ref spData); SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
For more information about C# related content, please check out the topic of this site:Summary of C# form operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.