I am using a program in this. win2008 + vs2008.
In fact, I have already disabled the NIC, but I cannot enable the disabled wireless NIC-_-B. However, it is okay to disable and enable the common physical Nic!
The reason why the wireless network card cannot be enabled is that the name when the wireless network card is disabled is different from the name when it is enabled.
Intel (r) Pro/wireless 3945abg network connection #2
However, the name obtained after disabling is Intel (r) Pro/wireless 3945abg network connection.
However, in the control panel-> network connection view, all of my wireless devices, whether disabled or enabled, are named Intel (r) Pro/wireless 3945abg network connection #2.
How can I get the correct Nic connection name at the bottom?
Code snippet
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Management;
Using system. collections;
Namespace networkcontrol
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
Networklist ();
}
/// <Summary>
/// Nic list
/// </Summary>
Public void networklist ()
{
String manage = "select * From win32_networkadapter ";
Managementobjectsearcher searcher = new managementobjectsearcher (manage );
Managementobjectcollection collection = searcher. Get ();
List <string> networklist = new list <string> ();
Foreach (managementobject OBJ in Collection)
{
Networklist. Add (OBJ ["name"]. tostring ());
}
This. cmbnetwork. datasource = networklist;
}
/// <Summary>
/// Disable the NIC
/// </Summary> 5
/// <Param name = "networkname"> Nic name </param>
/// <Returns> </returns>
Public bool disablenetwork (managementobject Network)
{
Try
{
Network. invokemethod ("Disable", null );
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Enable the NIC
/// </Summary>
/// <Param name = "networkname"> Nic name </param>
/// <Returns> </returns>
Public bool enablenetwork (managementobject Network)
{
Try
{
Network. invokemethod ("enable", null );
Return true;
}
Catch
{
Return false;
}
}
/// <Summary>
/// Nic status
/// </Summary>
/// <Param name = "networkname"> Nic name </param>
/// <Returns> </returns>
Public bool networkstate (string networkname)
{
String netstate = "select * From win32_networkadapter ";
Managementobjectsearcher searcher = new managementobjectsearcher (netstate );
Managementobjectcollection collection = searcher. Get ();
Foreach (managementobject manage in Collection)
{
If (manage ["name"]. tostring () = networkname)
{
Return true;
}
}
Return false;
}
/// <Summary>
/// Obtain the specified Nic
/// </Summary>
/// <Param name = "networkname"> Nic name </param>
/// <Returns> </returns>
Public managementobject Network (string networkname)
{
String netstate = "select * From win32_networkadapter ";
Managementobjectsearcher searcher = new managementobjectsearcher (netstate );
Managementobjectcollection collection = searcher. Get ();
Foreach (managementobject manage in Collection)
{
If (manage ["name"]. tostring () = networkname)
{
Return manage;
}
}
Return NULL;
}
Private void btnstart_click (Object sender, eventargs E)
{
If (networkstate (this. cmbnetwork. selectedvalue. tostring ()))
{
If (! Enablenetwork (Network (this. cmbnetwork. selectedvalue. tostring ())))
{
MessageBox. Show ("failed to enable Nic! ");
}
Else
{
MessageBox. Show ("Nic enabled successfully! ");
}
}
Else
{
MessageBox. Show ("Nic enabled! ");
}
Networklist ();
}
Private void btnstop_click (Object sender, eventargs E)
{
If (networkstate (this. cmbnetwork. selectedvalue. tostring ()))
{
If (! Disablenetwork (Network (this. cmbnetwork. selectedvalue. tostring ())))
{
MessageBox. Show ("failed to disable Nic! ");
}
Else
{
MessageBox. Show ("Nic disabled successfully! ");
}
}
Else
{
MessageBox. Show ("Nic disabled! ");
}
Networklist ();
}
}
}