国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現啟用與禁用本地網絡的方式小結【3種方式】

2019-10-29 21:24:28
字體:
來源:轉載
供稿:網友

本文實例總結了C#實現啟用與禁用本地網絡的方式。分享給大家供大家參考,具體如下:

1) 使用Hnetcfg.dll

使用Add Reference,把Hnetcfg.dll導入到工程中,會生成3個引用,主要使用NETCONLib。

在工程中要using NETCONLib;

下面是實現的代碼:

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(); //禁用網絡connection.Connect();  //啟用網絡}}

2) 使用Shell32.dll

shell32.dll是Windows殼Shell相關應用程序接口動態鏈接庫文件,用于打開網頁和文件。

使用Add Reference,把Shell32.dll導入到工程中。

在工程中要using Shell32;

下面是實現的代碼:

const string discVerb = "停用(&B)";const string connVerb = "啟用(&A)";Shell sh = new Shell32.Shell();Folder folder;Folder fd;folder = sh.NameSpace(3);foreach (FolderItem myItem in folder.Items()){if (myItem.Name == "網絡連接"){fd = (Folder)myItem.GetFolder;//禁用網絡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()){//啟用網絡if (Fib.Name == connVerb){Fib.DoIt();break;}}}}}

3) 使用setupapi.dll

setupapi.dll是流行的安裝程序支持相關文件

setupapi.dll不能象前面兩個通過Add Reference導入到工程中,只能使用DllImport

代碼比較多,貼主要代碼

[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_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);
 


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇州县| 山西省| 勃利县| 贵阳市| 托克逊县| 韩城市| 自贡市| 玉田县| 措勤县| 云南省| 潢川县| 诸城市| 伊春市| 恩平市| 黎平县| 木里| 绵竹市| 青州市| 榆林市| 嘉祥县| 濮阳市| 西宁市| 云安县| 岳阳县| 宝应县| 黄平县| 安多县| 汾阳市| 福州市| 和平县| 山阳县| 道孚县| 班戈县| 泰和县| 安陆市| 宣恩县| 渭源县| 柘荣县| 泾阳县| 南靖县| 温州市|