C#实现启用与禁用本地网络的方式小结【3种方式】
本文实例总结了C#实现启用与禁用本地网络的方式。分享给大家供大家参考,具体如下:
1)使用Hnetcfg.dll
使用AddReference,把Hnetcfg.dll导入到工程中,会生成3个引用,主要使用NETCONLib。
在工程中要usingNETCONLib;
下面是实现的代码:
NetSharingManagerClassnetSharingMgr=newNetSharingManagerClass();
INetSharingEveryConnectionCollectionconnections=netSharingMgr.EnumEveryConnection;
foreach(INetConnectionconnectioninconnections)
{
INetConnectionPropsconnProps=netSharingMgr.get_NetConnectionProps(connection);
if(connProps.MediaType==tagNETCON_MEDIATYPE.NCM_LAN)
{
connection.Disconnect();//禁用网络
connection.Connect();//启用网络
}
}
2)使用Shell32.dll
shell32.dll是Windows壳Shell相关应用程序接口动态链接库文件,用于打开网页和文件。
使用AddReference,把Shell32.dll导入到工程中。
在工程中要usingShell32;
下面是实现的代码:
conststringdiscVerb="停用(&B)";
conststringconnVerb="启用(&A)";
Shellsh=newShell32.Shell();
Folderfolder;
Folderfd;
folder=sh.NameSpace(3);
foreach(FolderItemmyIteminfolder.Items())
{
if(myItem.Name=="网络连接")
{
fd=(Folder)myItem.GetFolder;
//禁用网络
foreach(FolderItemfiinfd.Items())
{
foreach(FolderItemVerbFibinfi.Verbs())
{
if(Fib.Name==discVerb)
{
Fib.DoIt();
break;
}
}
Thread.Sleep(3000);
foreach(FolderItemVerbFibinfi.Verbs())
{
//启用网络
if(Fib.Name==connVerb)
{
Fib.DoIt();
break;
}
}
}
}
}
3)使用setupapi.dll
setupapi.dll是流行的安装程序支持相关文件
setupapi.dll不能象前面两个通过AddReference导入到工程中,只能使用DllImport
代码比较多,贴主要代码
[DllImport("setupapi.dll")]
publicstaticexternIntPtr
SetupDiGetClassDevsA(refGuidClassGuid,UInt32Enumerator,
IntPtrhwndParent,UInt32Flags);
[DllImport("setupapi.dll")]
publicstaticexternIntPtr
SetupDiGetClassDevs(UInt32ClassGuid,Stringe,IntPtrhwndParent,UInt32Flags);
[DllImport("setupapi.dll")]
staticexternBoolean
SetupDiEnumDeviceInfo(IntPtrDeviceInfoSet,UInt32MemberIndex,
refSP_DEVINFO_DATADeviceInfoData);
…………
uintNewNetStatus=0;
if(newStatus)
NewNetStatus=DICS_ENABLE;
else
NewNetStatus=DICS_DISABLE;
IntPtrNewDeviceInfoSet;
SP_DEVINFO_DATAspData=newSP_DEVINFO_DATA();
spData.cbSize=(uint)System.Runtime.InteropServices.Marshal.SizeOf(spData);
UInt32RequiredSize=0;
byte[]st1=newbyte[1024];
uintData=0;
NewDeviceInfoSet=SetupDiGetClassDevs(0,"PCI",IntPtr.Zero,DIGCF_PRESENT|DIGCF_ALLCLASSES);
boolbFound=false;
for(uinti=0;SetupDiEnumDeviceInfo(NewDeviceInfoSet,i,refspData);i++)
{
while(!SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet,refspData,SPDRP_HARDWAREID,refData,st1,1024,refRequiredSize))
{
}
stringstr=System.Text.Encoding.ASCII.GetString(st1);;
char[]a={'/0'};
string[]strSPlit=str.Split(a,StringSplitOptions.RemoveEmptyEntries);
stringHardId=@"PCI/VEN_10EC&DEV_8029&SUBSYS_00000000";
for(uintj=0;j<strSPlit.Length;j++)
{
if(strSPlit[j]==HardId)
{
bFound=true;
break;
}
}
if(bFound)
break;
}
SP_PROPCHANGE_PARAMSspPropChangeParam=newSP_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,refspData,refspPropChangeParam.ClassInstallHeader,System.Runtime.InteropServices.Marshal.SizeOf(spPropChangeParam));
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,NewDeviceInfoSet,refspData);
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#程序设计之线程使用技巧总结》、《C#操作Excel技巧总结》、《C#中XML文件操作技巧汇总》、《C#数据结构与算法教程》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。