Enable and disable Nic in C #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 ();
}
}
}