乙個簡單的埠占用查詢工具

2021-09-18 07:36:39 字數 2888 閱讀 3888

有時做tcp開發,會遇到埠被占用,就得用netstat命令查pid,再根據pid查程序.比較浪費時間;

可用winform實現乙個簡單的tcp埠占用查詢工具,並提供殺程序的方法;

先實現查詢類:

using system;

using system.collections.generic;

using system.diagnostics;

using system.net;

namespace util

/// /// 根據netstat命令找到占用埠的程序id,並返回占用程序物件資訊

///

/// 埠

///

public static portuserinfo netstatus(int port,bool strict=false)

\"&exit";

process p = new process();

//設定要啟動的應用程式

p.startinfo.filename = "cmd.exe";

//是否使用作業系統shell啟動

p.startinfo.useshellexecute = false;

// 接受來自呼叫程式的輸入資訊

p.startinfo.redirectstandardinput = true;

//輸出資訊

p.startinfo.redirectstandardoutput = true;

// 輸出錯誤

p.startinfo.redirectstandarderror = true;

//不顯示程式視窗

p.startinfo.createnowindow = true;

//啟動程式

p.start();

//向cmd視窗傳送輸入資訊

p.standardinput.writeline(strinput);

p.standardinput.autoflush = true;

//獲取輸出資訊

string strouput = p.standardoutput.readtoend();

//等待程式執行完退出程序

p.waitforexit();

p.close();

int index = strouput.indexof(strinput);

string reply = strouput.remove(0, index + strinput.length);

listprocesses = new list();

string sp = new string ;

string infos = reply.split(sp, stringsplitoptions.removeemptyentries);

foreach (string line in infos)

;string column = line.split(sp, stringsplitoptions.removeemptyentries);

if (column.length >= 5)

if (int.tryparse(portstring, out int localport))

if (strict)

}pinfo.remoteendpoint = column[2];

pinfo.state = column[3];

string pidstring = column[4];

if (int.tryparse(pidstring, out int pid))}}

return processes.toarray();}}

}

返回查詢內容後,顯示在列表上;

設計檢視**不給出,根據個人需求自行設計;

引用方法例項:

using system;

using system.collections.generic;

using system.componentmodel;

using system.data;

using system.diagnostics;

using system.drawing;

using system.linq;

using system.text;

using system.threading.tasks;

using system.windows.forms;

using util;

namespace debugtool

private void btnsearch_click(object sender, eventargs e)

);datagridview1.rows[index].tag = p.process;}}

private void datagridview1_cellcontentclick(object sender, datagridviewcelleventargs e)

if (datagridview1.columns[col]==column6)

]:[pid=]嗎?\r\n請確認結束程序的不會帶來嚴重後果.";

var dr = messagebox.show(msg,"警告",messageboxbuttons.okcancel,messageboxicon.warning);

if (dr == dialogresult.ok)}}}}}

工具完成;

查詢被占用的埠

3.ctrl alt del開啟任務管理器,選程序,這裡有很多正在執行的程式怎麼找?別急點上面的 檢視 選擇列 在pid 程序標示符 前面打鉤。好了,下面的程序前面都有了pid號碼。這時上 一步找到的pid就有用了,找到1484,比如peer.exe什麼的,結束程序吧。這時再開伺服器,看 web可以...

乙個簡單的查詢語句

今天要寫乙個查詢語句,寫了半天沒弄出來,最後在同事的幫助下,用group by搞定了。其實蠻簡單的,只是我用資料庫用的太少了,呵呵 物品表 good 欄位為gid 物品id price 物品 和tid 所屬交易id 交易表 trade,欄位為tid 交易id date 交易時間 需要寫乙個查詢語句,...

查詢電腦占用埠的程式

netstat ano 我們可以知道某一埠被那個程序 對應pid 占用 然後我們可以開啟任務管理器 檢視某一pid對應的程序名 如果pid沒有顯示,選單 檢視 選擇列 選中pid即可 得知程序後,我們可以將程序殺掉 修改其使用的埠。假如我們需要確定誰占用了我們的8080埠 在windows命令列視窗...