C 一鍵顯示及殺死占用埠號程序

2022-02-01 11:50:50 字數 2030 閱讀 2401

private

void t_btn_kill_click(object

sender, eventargs e)

process p = new

process();

p.startinfo.filename = "

cmd.exe";

p.startinfo.useshellexecute = false

; p.startinfo.redirectstandarderror = true

; p.startinfo.redirectstandardinput = true

; p.startinfo.redirectstandardoutput = true

; p.startinfo.createnowindow = true

; list

list_pid =getpidbyport(p, port);

if (list_pid.count == 0

)

list

list_process =getprocessnamebypid(p, list_pid);

stringbuilder sb = new

stringbuilder();"占用

" + port + "

埠的程序有:");

foreach (var item in

list_process)

"是否要結束這些程序?");

if (messagebox.show(sb.tostring(), "

系統提示

", messageboxbuttons.yesno) ==dialogresult.no)

return

; pidkill(p, list_pid);

messagebox.show(

"操作完成");

}private

static

void pidkill(process p, listlist_pid)

p.close();

}private

static list getpidbyport(process p, int

port)

\"", port));

p.standardinput.writeline(

"exit");

streamreader reader =p.standardoutput;

string strline =reader.readline();

list

list_pid = new list();

while (!reader.endofstream)

}strline =reader.readline();

}p.waitforexit();

reader.close();

p.close();

return

list_pid;

}private

static list getprocessnamebypid(process p, listlist_pid)

\"", pid));

p.standardinput.writeline(

"exit");

streamreader reader = p.standardoutput;//

擷取輸出流

string strline = reader.readline();//

每次讀取一行

while (!reader.endofstream)

}strline =reader.readline();

}p.waitforexit();

reader.close();

}p.close();

return

list_process;

}

c#實現檢查指定埠被哪個程序占用

C 一鍵顯示及殺死占用埠號程序

private void t btn kill click object sender,eventargs e process p new process p.startinfo.filename cmd.exe p.startinfo.useshellexecute false p.startin...

ubuntu一鍵安裝LAMP 及一鍵解除安裝

一鍵安裝lamp服務 sudo tasksel install lamp server 一鍵解除安裝lamp sudo tasksel remove lamp server 注意 lamp linux apache mysql php 通過上面的命令解除安裝lamp時不免把linux系統本身的東西解...

C 一鍵關閉啟動服務,一鍵禁用服務

登錄檔操作 public class gf regreadwrite catch 設定路徑為keypath,鍵名為keyname的登錄檔鍵值為keyval static public bool setregval registrykey rootkey,string keypath,string k...