C#实现启用与禁用本地网络的方式小结【3种方式】
作者:饅頭 时间:2022-04-21 18:45:14
本文实例总结了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#程序设计有所帮助。
标签:C#,本地网络
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java编程探索之泛型擦除实例解析
2022-08-30 02:13:35
C#中加载dll并调用其函数的实现方法
2022-06-27 17:14:08
![](https://img.aspxhome.com/file/2023/5/111165_0s.jpg)
Netty分布式高性能工具类同线程下回收对象解析
2023-05-24 22:00:55
C# 函数返回多个值的方法详情
2022-01-05 05:47:11
详解C#借助.NET框架中的XmlTextReader类读取XML的方法
2023-01-18 23:31:47
![](https://img.aspxhome.com/file/2023/8/75578_0s.jpg)
带大家认识Java语法之泛型与通配符
2021-06-04 06:14:46
![](https://img.aspxhome.com/file/2023/4/67494_0s.png)
Java后端学习精华之TCP通信传输协议详解
2021-08-06 03:15:42
![](https://img.aspxhome.com/file/2023/1/64221_0s.png)
java获取当前时间并格式化代码实例
2021-10-06 17:06:16
![](https://img.aspxhome.com/file/2023/0/79240_0s.png)
c#简单读取文本的实例方法
2022-02-16 14:51:52
SpringMVC源码解读之 HandlerMapping - AbstractDetectingUrlHandlerMapping系列初始化
2023-02-12 16:14:21
Android仿微信单击拍照长按录像功能实例代码
2022-01-16 17:45:07
C#中ExecuteNonQuery()返回值注意点分析
2023-04-05 03:40:14
springboot如何统一设置时区
2022-01-26 18:16:39
![](https://img.aspxhome.com/file/2023/9/63659_0s.png)
简略分析Android的Retrofit应用开发框架源码
2023-09-17 22:12:30
![](https://img.aspxhome.com/file/2023/4/93304_0s.png)
C# 注册表 操作实现代码
2023-07-02 21:53:22
Java 8中的18个常用日期处理(收藏)
2023-03-02 02:01:54
![](https://img.aspxhome.com/file/2023/1/66491_0s.jpg)
c#的时间日期操作示例分享(c#获取当前日期)
2021-07-09 15:44:35
javaWeb 四大域对象详细介绍
2021-08-16 14:04:01
Java SpringCache+Redis缓存数据详解
2023-11-29 01:01:05
![](https://img.aspxhome.com/file/2023/1/60131_0s.jpg)
Spingboot JPA CriteriaBuilder 如何获取指定字段
2022-07-20 15:40:22