Refer to the park friend freeliver54 code and do, and then according to demand, rookie one only problem despite the proposed
----------------------------------------------------------------------
1 Private voidT_btn_kill_click (Objectsender, EventArgs e)2 {3 intPort;4 BOOLb =int. TryParse (T_txt_guardport. Text, outport);5 if(!b)6 {7MessageBox.Show ("Please enter the correct listening port");8 return;9 }TenProcess p =NewProcess (); Onep.StartInfo.FileName ="Cmd.exe"; AP.startinfo.useshellexecute =false; -P.startinfo.redirectstandarderror =true; -P.startinfo.redirectstandardinput =true; theP.startinfo.redirectstandardoutput =true; -P.startinfo.createnowindow =true; -list<int> list_pid =Getpidbyport (P, port); - if(List_pid. Count = =0) + { -MessageBox.Show ("Operation completed"); + return; A } atlist<string> list_process =getprocessnamebypid (P, list_pid); -StringBuilder SB =NewStringBuilder (); -Sb. Appendline ("occupy"+ Port +"the processes for ports are:"); - foreach(varIteminchlist_process) - { -Sb. Append (item +"\ r \ n"); in } -Sb. Appendline ("do you want to end these processes? "); to + if(MessageBox.Show (sb.) ToString (),"System Tips", messageboxbuttons.yesno) = =dialogresult.no) - return; the Pidkill (P, list_pid); *MessageBox.Show ("Operation completed"); $ Panax Notoginseng } - the Private Static voidPidkill (Process p, list<int>list_pid) + { A P.start (); the foreach(varIteminchlist_pid) + { -P.standardinput.writeline ("Taskkill/pid"+ Item +"/ F"); $P.standardinput.writeline ("Exit"); $ } - p.close (); - } the - Private Staticlist<int> Getpidbyport (Process p,intPort)Wuyi { the intresult; - BOOLb =true; Wu P.start (); -P.standardinput.writeline (string. Format ("netstat-ano|find \ "{0}\"", Port)); AboutP.standardinput.writeline ("Exit"); $StreamReader reader =P.standardoutput; - stringStrLine =Reader. ReadLine (); -list<int> list_pid =Newlist<int>(); - while(!Reader. Endofstream) A { +StrLine =Strline.trim (); the if(Strline.length >0&& (Strline.contains ("TCP") || Strline.contains ("UDP")))) - { $Regex r =NewRegex (@"\s+"); the string[] Strarr =R.split (strLine); the if(Strarr.length >=4) the { theb =int. TryParse (strarr[3], outresult); - if(b &&!)List_pid. Contains (Result)) in List_pid. ADD (result); the } the } AboutStrLine =Reader. ReadLine (); the } the p.waitforexit (); the Reader. Close (); + p.close (); - returnList_pid; the }Bayi the Private Staticlist<string> Getprocessnamebypid (Process p, list<int>list_pid) the { - P.start (); -list<string> list_process =Newlist<string>(); the foreach(varPidinchlist_pid) the { theP.standardinput.writeline (string. Format ("tasklist |find \ "{0}\"", PID)); theP.standardinput.writeline ("Exit"); -StreamReader reader = p.standardoutput;//Intercept output Stream the stringStrLine = reader. ReadLine ();//read one line at a time the the while(!Reader. Endofstream)94 { theStrLine =Strline.trim (); the if(Strline.length >0&& (Strline.contains (". EXE")))) the {98Regex r =NewRegex (@"\s+"); About string[] Strarr =R.split (strLine); - if(Strarr.length >0)101 {102List_process. ADD (strarr[0]);103 }104 } theStrLine =Reader. ReadLine ();106 }107 p.waitforexit ();108 Reader. Close ();109 } the p.close ();111 the returnlist_process;113}
C # A key to display and kill the occupied port number process