开发者

Even after uninstalling windows service, port number remains still occupied

开发者 https://www.devze.com 2023-01-27 15:24 出处:网络
I am installing windows service and it works fine. Now I am uninstalling the same service. (to be specific I am using installutil command for installing as well as uninstalling) The service gets unin

I am installing windows service and it works fine.

Now I am uninstalling the same service. (to be specific I am using installutil command for installing as well as uninstalling) The service gets uninstalled but when I go to command prompt and check the status开发者_Python百科 of the port it shows the port is still occupied. (Using netstat command)

Due to this when I try to delete the folder containing the service some of the dlls are not getting deleted and on trying to delete them forcefully I get the message already in user.

Can someone guide on this.


Use netstat -b to determine which executable is occupying your port, then kill it using task manager with the "Show processes from all users" option enabled.


Finally after doing testing different combination with netstat, I came up with this code

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

and then to kill that process

private static void KillProcess(int PID)
    {
        try
        {
            Process p = Process.GetProcessById(PID);
            p.Kill();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message + "\n" + e.StackTrace);
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号