In C #, how do I disable and enable wireless network interfaces?

Source: Internet
Author: User

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 ();
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.