我正在安裝Windows服務,它工作正常。即使卸載Windows服務後,端口號仍然被佔用
現在我正在卸載相同的服務。 (具體而言,我正在使用installutil命令進行安裝和卸載)服務被卸載,但是當我進入命令提示符並檢查端口狀態時,它顯示端口仍然被佔用。 (使用netstat命令)
由於這種情況,當我嘗試刪除包含該服務的文件夾時,某些dll沒有被刪除,並試圖強制刪除它們,我得到的消息已經在用戶中。
有人可以在此指導。
我正在安裝Windows服務,它工作正常。即使卸載Windows服務後,端口號仍然被佔用
現在我正在卸載相同的服務。 (具體而言,我正在使用installutil命令進行安裝和卸載)服務被卸載,但是當我進入命令提示符並檢查端口狀態時,它顯示端口仍然被佔用。 (使用netstat命令)
由於這種情況,當我嘗試刪除包含該服務的文件夾時,某些dll沒有被刪除,並試圖強制刪除它們,我得到的消息已經在用戶中。
有人可以在此指導。
最後,我想出了這個代碼
private static int GetProcessId(string portno)
{
string command = "netstat -o -n -a | findstr 0.0:" + portno;
ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
procStartInfo.CreateNoWindow = true;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
StreamReader sr1 = proc.StandardOutput;
string PID = sr1.ReadLine();
int index = PID.LastIndexOf(" ") + 1;
PID = PID.Substring(index, (PID.Length - (index--)));
return Convert.ToInt32(PID);
}
,然後殺掉該進程
private static void KillProcess(int PID)
{
try
{
Process p = Process.GetProcessById(PID);
p.Kill();
}
catch (Exception e)
{
MessageBox.Show(e.Message + "\n" + e.StackTrace);
}
}
使用netstat -b來確定哪個可執行文件佔用了您的端口,然後使用任務管理器並啓用「顯示所有用戶的進程」選項來終止它。做用netstat測試不同的組合後
能否請您詳細說明你的答案並命令我使用我沒有得到你的意思 – 2010-12-01 17:33:21