C#完成啟用與禁用當地收集的方法小結【3種方法】。本站提示廣大學習愛好者:(C#完成啟用與禁用當地收集的方法小結【3種方法】)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成啟用與禁用當地收集的方法小結【3種方法】正文
本文實例總結了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 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);
願望本文所述對年夜家C#法式設計有所贊助。