C # enable or disable the local network

Source: Internet
Author: User

1) Use hnetcfg. dll

Use add reference to import hnetcfg. DLL to the project. Three references are generated, mainly using netconlib. In the project, using netconlib is required. The following code is implemented: netsharingmanagerclass netsharingmgr = new netsharingmanagerclass (); inetsharingeveryconnectioncollection connections = netsharingmgr. enumeveryconnection; foreach (inetconnection connection in connections) {inetconnectionprops connprops = netsharingmgr. get_netconnectionprops (connection); If (connprops. mediatype = tagnetcon_mediatype.ncm_lan) {connection. disconnect (); // disable network connection. connect (); // enable the network

} 2) using shell32.dllshell32. dll is a Windows Shell application interface that dynamically links library files to open webpages and files. Use add reference to import shell32.dll to the project. In the project, using shell32 is required. The following code is implemented: const string discverb = "disabled (& B)"; const string connverb = "enabled (& )"; shell SH = new shell32.shell (); folder; Folder FD; Folder = Sh. namespace (3); foreach (folderitem myitem in folder. items () {If (myitem. name = "network connection") {FD = (folder) myitem. getfolder; // disable network foreach (folderitem fi in FD. items () {foreach (folderitemverb fib in FI. verbs () {If (fib. name = discverb) {fib. doit (); Break;} thread. sleep (3000); foreach (folderitemverb fib in FI. verbs () {// enable the network if (fib. name = connverb) {fib. doit (); break ;}}}3) Use setupapi. DLL setupapi. DLL is a popular installer that supports related files setupapi. DLL cannot be imported to the project like the previous two using Add reference. You can only use dllimport for more code and paste the main code [dllimport ("setupapi. DLL ")] public static extern intptrsetupdigetclassdevsa (ref guid classguid, uint32 enumerator, intptr hwndparent, uint32 flags); [dllimport (" setupapi. DLL ")] public static extern intptrsetupdigetclassdevs (uint32 classguid, string E, intptr hwndparent, uint32 flags); [dllimport (" setupapi. DLL ")] Static extern booleansetupdienumdeviceinfo (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 (); spdata. cbsize = (uint) system. runtime. interopservices. marshal. sizeof (spdata); uint32 requiredsize = 0; byte [] ST1 = new byte [1024]; uint DATA = 0; newdeviceinfoset = setupdigetclassdevs (0, "PCI", intptr. zero, digcf_present | digcf_allclass Es); 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 = system. text. encoding. ASCII. getstring (ST1); char [] A = {'\ 0'}; string [] strsplit = Str. split (A, stringsplitoptions. removeemptyentries); string hardid = @ "PCI \ ven_10ec & dev_8029 & subsys_00000000"; for (uint J = 0; j <strsplit. length; j ++) {If (strsplit [J] = hardid) {bfound = true; break ;}} if (bfound) break ;} sp_propchange_params sppropchangeparam = new sp_propchange_params (); sppropchangeparam. scope = dics_flag_global; sppropchangeparam. statechange = newnetstatus; sppropchangeparam. classinstallheader. cbsize = (uint32) system. runtime. interopservices. marshal. sizeof (sppropchangeparam. classinstallheader); sppropchangeparam. classinstallheader. installfunction = dif_propertychange; setupdisetclassinstallparams (newdeviceinfoset, ref spdata, ref sppropchangeparam. classinstallheader, system. runtime. interopservices. marshal. sizeof (sppropchangeparam); setupdicallclassinstaller (dif_propertychange, newdeviceinfoset, ref spdata );

Setupdidestroydeviceinfolist (newdeviceinfoset );

Related Article

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.