当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现启用与禁用本地网络的方式小结【3种方式】

C#实现启用与禁用本地网络的方式小结【3种方式】

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例总结了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#窗体操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网