【问题标题】:Even after uninstalling windows service, port number remains still occupied卸载windows服务后,端口号仍然被占用
【发布时间】:2010-12-01 13:39:34
【问题描述】:

我正在安装 windows 服务,它工作正常。

现在我正在卸载相同的服务。 (具体来说,我使用 installutil 命令进行安装和卸载)该服务被卸载,但是当我转到命令提示符并检查端口的状态时,它显示端口仍然被占用。 (使用 netstat 命令)

因此,当我尝试删除包含服务的文件夹时,一些 dll 没有被删除,而在尝试强制删除它们时,我已经在用户中收到消息。

有人可以指导一下吗?

【问题讨论】:

    标签: windows-services service serial-port uninstallation


    【解决方案1】:

    使用 netstat -b 确定哪个可执行文件占用了您的端口,然后使用启用了“显示所有用户的进程”选项的任务管理器将其终止。

    【讨论】:

    • 您能否详细说明您要使用的答案和命令,因为我没有完全理解您的意思
    【解决方案2】:

    最后在用 netstat 测试不同的组合后,我想出了这段代码

    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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-08
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 2011-10-25
      • 1970-01-01
      • 2021-09-03
      相关资源
      最近更新 更多