C # Implementation enable and disable local network summary of the "3 Ways" _c# Tutorial

Source: Internet
Author: User

The example in this article summarizes how the C # implementation enables and disables the local network. Share to everyone for your reference, specific as follows:

1) using Hnetcfg.dll

Using Add Reference, import Hnetcfg.dll into the project, generate 3 references, mainly using Netconlib.

In the project to be using Netconlib;

Here is the implementation code:

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 (); Disables network
connection. Connect ();  Enable Network
}
}

2) using Shell32.dll

Shell32.dll is a Windows Shell Shell related application Interface Dynamic link library file that is used to open Web pages and files.

Use the Add Reference to import the Shell32.dll into the project.

In the project to be using Shell32;

Here is the implementation code:

Const string Discverb = "Deactivate (&b)";
Const string Connverb = "enabled (&a)";
Shell sh = new Shell32.shell ();
Folder 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 network
if (fib.name = = Connverb)
{
fib.doit ();
Break;}}}}

3) using Setupapi.dll

Setupapi.dll is a popular setup program supporting related files

Setupapi.dll cannot be imported into the project like the previous two through Add reference, only using the DllImport

Code more, paste the main code

[DllImport ("Setupapi.dll")] public static extern IntPtr SETUPDIGETCLASSDEVSA (ref Guid Classguid, UInt32 Enumerator, INTP
TR hwndparent, UInt32 Flags); [DllImport ("Setupapi.dll")] public static extern IntPtr Setupdigetclassdevs (UInt32 classguid, String E, IntPtr
hWndParent, UInt32 Flags); [DllImport ("Setupapi.dll")] 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 ();
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_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 = 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);

Read more about C # Interested readers can view the site topics: "C # form Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Programming Thread Usage Skills summary", "C # Operation Excel Skills Summary", "C # Summary of operational skills in XML files, C # tutorial on data structure and algorithms, C # array operation techniques Summary, and C # Introduction to object-oriented Programming

I hope this article will help you with C # programming.

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.