您的位置:首页 > 理论基础 > 计算机网络

C#实现启用、禁用本地网络的三种方式

2009-04-23 20:37 645 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: