C # language implementation timed on/off NIC
- Program run
- Program implementation Main code
- Source Code Engineering file (VS2013 project file compiled by)
- View network card name drawings
1.win7 flagship version runs:
2. Program implementation of the main code:
Nic List
1//<summary> 2// net Carré table 3// </summary> 4 public void {6 string manage = "SEL ECT * from Win32_NetworkAdapter "; 7 ManagementObjectSearcher searcher = new ManagementObjectSearcher ( Manage); 8 managementobjectcollection collection = searcher. Get (); 9 list<string> networklist = new list<string>(); foreach (ManagementObject obj in {net Worklist.add (obj["Name" }16 this.cmbNetWork.DataSource = networklist;17 )
Disable NIC
1//<summary> 2/// disable NIC 3// </summary>5 4// <param name= "NetworkName" > Nic name < /param> 5// <returns></returns> 6 public bool {8 {ten Network. InvokeMethod ("Disable", null), one return True {return false }17}
Enable NIC
1// <summary> 2// enable NIC 3// </summary> 4// <param name= "NetworkName" > Nic name </ Param> 5// <returns></returns> 6 public bool {8 {ten Network. InvokeMethod ("Enable", null); return True {return false }17}
Nic Status
1//<summary> 2/// network card Status 3// </summary> 4// <param name= "NetworkName" > Nic name </ Param> 5 //<returns></returns> 6 public bool Networkstate (string {8 string netstate = "SELECT * from Win32_NetworkAdapter"; 9 ManagementObjectSearcher searcher = new ManagementObjectSearcher (netstate), managementobjectcollection collection = searcher. Get (), one-by-one foreach (ManagementObject manage in {manage["Name"]. ToString () = = {return true }18 return false;
Get the specified NIC
1// <summary> 2// get designated NIC 3// </summary> 4// <param name= "NetworkName" > Nic name </param> 5 //<returns></returns> 6 public managementobject NetWork (string { 8 String netstate = "SELECT * from Win32_NetworkAdapter"; 9 ManagementObjectSearcher searcher = new ManagementObjectSearcher (netstate); managementobjectcollection collection = searcher. Get (); ManagementObject Manage in {if (manage["Name"]. ToString () = = { }20 return null;
Turn on the NIC
1 //<summary> 2 ///Open NIC 3 //</summary> 4 public bool {6 if ( Networkstate (This {8 if (! Enablenetwork (NetWork (this {ten). Insertlog ("Failed to turn on the NIC!") ); return false {this. Insertlog ("Turn on NIC success!") ), and return True {. Insertlog ("network card has been turned on!" ); return false }24//networklist ();
Turn off the NIC
1 //<summary> 2///close NIC 3 //</summary> 4 public bool {6 if ( Networkstate (This {8 if (! Disablenetwork (NetWork (this {ten). Insertlog ("Disable NIC failed!") ); return false {this. Insertlog ("Disable NIC success!") ), and return True {. Insertlog ("NIC is disabled!") ); return false }24//networklist ();
Log records
1// <summary> 2// logging 3//</summary> 4// <param name= "msg" ></param> 5< c4/>public void Insertlog (string {7 if (This.richTextBox1.Lines.Count () > }11 String str = String.Format ("[{0}]--[{1}]-->{2}", DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss"), This.cmbNetWork.Text.Trim (), msg) + environment.newline;12 richtextbox1.text = RichTextBox1.Text.Insert (0, STR);
Turn on or off regularly
1 private int startTime = 0; 2 3 //<summary> 4//timed on/off 5// </summary> 6 <param name= "Sender" ></param> 7// <param name= "E" ></param> 8 private void timer1 _tick (Object {ten if (startTime! = { insertlog (time-starttime-1) + "seconds after Execution"); 13 starttime++return }16 startTime = 0 {if (this {_state = 1 } if (this {_sta Te = 0 }32}
3. Project source file download
Attachment: Project source file Http://files.cnblogs.com/files/JiYF/NetWorkCardConnect.rar
4. Check the NIC name
C # language implementation timed to turn on or disable the NIC applet