C#實(shí)現(xiàn)啟用與禁用本地網(wǎng)絡(luò)的方式小結(jié)【3種方式】
本文實(shí)例總結(jié)了C#實(shí)現(xiàn)啟用與禁用本地網(wǎng)絡(luò)的方式。分享給大家供大家參考,具體如下:
1) 使用Hnetcfg.dll
使用Add Reference,把Hnetcfg.dll導(dǎo)入到工程中,會(huì)生成3個(gè)引用,主要使用NETCONLib。
在工程中要using NETCONLib;
下面是實(shí)現(xiàn)的代碼:
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(); //禁用網(wǎng)絡(luò) connection.Connect(); //啟用網(wǎng)絡(luò) } }
2) 使用Shell32.dll
shell32.dll是Windows殼Shell相關(guān)應(yīng)用程序接口動(dòng)態(tài)鏈接庫(kù)文件,用于打開網(wǎng)頁和文件。
使用Add Reference,把Shell32.dll導(dǎo)入到工程中。
在工程中要using Shell32;
下面是實(shí)現(xiàn)的代碼:
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 == "網(wǎng)絡(luò)連接") { fd = (Folder)myItem.GetFolder; //禁用網(wǎng)絡(luò) 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()) { //啟用網(wǎng)絡(luò) if (Fib.Name == connVerb) { Fib.DoIt(); break; } } } } }
3) 使用setupapi.dll
setupapi.dll是流行的安裝程序支持相關(guān)文件
setupapi.dll不能象前面兩個(gè)通過Add Reference導(dǎo)入到工程中,只能使用DllImport
代碼比較多,貼主要代碼
[DllImport("setupapi.dll")] public static extern IntPtr SetupDiGetClassDevsA(ref Guid ClassGuid, UInt32 Enumerator, IntPtr 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);
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
- C#設(shè)置本地網(wǎng)絡(luò)如DNS、網(wǎng)關(guān)、子網(wǎng)掩碼、IP等等
- C#中判斷本地系統(tǒng)的網(wǎng)絡(luò)連接狀態(tài)的方法
- c#多線程網(wǎng)絡(luò)聊天程序代碼分享(服務(wù)器端和客戶端)
- c#網(wǎng)絡(luò)喚醒功能實(shí)現(xiàn)
- c#判斷網(wǎng)絡(luò)連接狀態(tài)
- C#同步網(wǎng)絡(luò)時(shí)間的方法實(shí)例詳解
- C#網(wǎng)絡(luò)編程基礎(chǔ)之進(jìn)程和線程詳解
- C#實(shí)現(xiàn)從網(wǎng)絡(luò)同步標(biāo)準(zhǔn)北京時(shí)間的方法
- c#判斷網(wǎng)絡(luò)連接狀態(tài)的示例分享
相關(guān)文章
UnityShader3實(shí)現(xiàn)2D描邊效果
這篇文章主要為大家詳細(xì)介紹了UnityShader3實(shí)現(xiàn)2D描邊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02詳解C#實(shí)現(xiàn)在Excel單元格中應(yīng)用多種字體格式
在Excel中,可對(duì)單元格中的字符串設(shè)置多種不同樣式。本文,將以C#及VB.NET代碼為例,介紹如何在Excel同一個(gè)單元格中應(yīng)用多種字體樣式,感興趣的可以了解一下2022-05-05使用C#實(shí)現(xiàn)將Word?轉(zhuǎn)文本存儲(chǔ)到數(shù)據(jù)庫(kù)并進(jìn)行管理
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)將Word?轉(zhuǎn)文本存儲(chǔ)到數(shù)據(jù)庫(kù)并進(jìn)行管理,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2024-03-03C#實(shí)現(xiàn)六大設(shè)計(jì)原則之里氏替換原則
這篇文章介紹了C#實(shí)現(xiàn)六大設(shè)計(jì)原則之里氏替換原則的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02