How to kill the application that is using a TCP port in C#?

7.2k views Asked by At

I want to free a TCP port during startup of my application (asking confirmation to user), how to get the PID number and then, if the user confirm, kill it?

I know I can get this information by netstat, but how to do it in a script or better in a C# method.

2

There are 2 answers

0
yohannist On

You can run netstat then redirect the output to a text stream so you can parse and get the info you want.

Here is what i did.

  • Run netstat -a -n -o as a Process
  • redirect the standard out put and capture the output text
  • capture the result, parse and return all the processes in use
  • check if the port is being used
  • find the process using linq
  • Run Process.Kill()

you will have to do the exception handling.

namespace test
{
      static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            
            static void Main()
            {
                
                Console.WriteLine("Port number you want to clear");
                var input = Console.ReadLine();
                //var port = int.Parse(input);
                var prc = new ProcManager();
                prc.KillByPort(7972); //prc.KillbyPort(port);
    
            }
        }
    
     
    
    public class PRC
     {
            public int PID { get; set; }
            public int Port { get; set; }
            public string Protocol { get; set; }
     }
        public class ProcManager
        {
            public void KillByPort(int port)
            {
                var processes = GetAllProcesses();
                if (processes.Any(p => p.Port == port))
                 try{
                    Process.GetProcessById(processes.First(p => p.Port == port).PID).Kill();
                    }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                else
                {
                    Console.WriteLine("No process to kill!");
                }
            }
    
            public List<PRC> GetAllProcesses()
            {
                var pStartInfo = new ProcessStartInfo();
                pStartInfo.FileName = "netstat.exe";
                pStartInfo.Arguments = "-a -n -o";
                pStartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                pStartInfo.UseShellExecute = false;
                pStartInfo.RedirectStandardInput = true;
                pStartInfo.RedirectStandardOutput = true;
                pStartInfo.RedirectStandardError = true;
    
                var process = new Process()
                {
                    StartInfo = pStartInfo
                };
                process.Start();
    
                var soStream = process.StandardOutput;
                
                var output = soStream.ReadToEnd();
                if(process.ExitCode != 0)
                    throw new Exception("somethign broke");
    
                var result = new List<PRC>(); 
                    
               var lines = Regex.Split(output, "\r\n");
                foreach (var line in lines)
                {
                    if(line.Trim().StartsWith("Proto"))
                        continue;
                    
                    var parts = line.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
    
                    var len = parts.Length;
                    if(len > 2)
                        result.Add(new PRC
                        {
                            Protocol = parts[0],
                            Port = int.Parse(parts[1].Split(':').Last()),
                            PID = int.Parse(parts[len - 1])
                        });
                   
                 
                }
                return result;
            }
        }
}
0
Matěj Štágl On

Improved version of @yohannist's answer (missing using, unnecessary GUI window and some other things). The following code can be used as:

ProcMgr.KillWherePort(port)

Implementation:

public class DataOrException<T>
{
    public T? Data { get; set; }
    public Exception? Exception { get; set; }

    public DataOrException(T? data)
    {
        Data = data;
    }

    public DataOrException(Exception? exception)
    {
        Exception = exception;
    }
}

public enum ProcKillResult
{
    Ok,
    PortEmpty
}

public class PRC
{
    public int PID { get; set; }
    public int Port { get; set; }
    public string Protocol { get; set; }
}

public static class ProcMgr
{
    public static DataOrException<ProcKillResult> KillWherePort(int port)
    {
        DataOrException<List<PRC>> processes = GetAllProcesses();

        if (processes.Data is null || processes.Exception is not null)
        {
            return new DataOrException<ProcKillResult>(processes.Exception);
        }
        
        PRC? offendingProcess = processes.Data.FirstOrDefault(x => x.Port == port);
        
        if (offendingProcess is not null)
        {
            try
            {
                Process.GetProcessById(offendingProcess.PID).Kill();
                return new DataOrException<ProcKillResult>(ProcKillResult.Ok);
            }
            catch (Exception ex)
            {
                return new DataOrException<ProcKillResult>(ex);
            }
        }
        
        return new DataOrException<ProcKillResult>(ProcKillResult.PortEmpty);
    }

    private static readonly Regex newlineRegex = new Regex(Environment.NewLine, RegexOptions.Compiled);

    public static DataOrException<List<PRC>> GetAllProcesses()
    {
        ProcessStartInfo pStartInfo = new ProcessStartInfo
        {
            FileName = "netstat.exe",
            Arguments = "-a -n -o",
            CreateNoWindow = true,
            UseShellExecute = false,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true
        };

        using Process process = new Process();
        process.StartInfo = pStartInfo;
        process.Start();
        StreamReader soStream = process.StandardOutput;
        string output = soStream.ReadToEnd();

        return process.ExitCode is not 0 ? new DataOrException<List<PRC>>(new Exception($"Netstat result is {process.ExitCode}, expected 0")) : new DataOrException<List<PRC>>((from line in newlineRegex.Split(output) where !line.Trim().StartsWith("Proto") select line.Split(' ', StringSplitOptions.RemoveEmptyEntries) into parts let len = parts.Length where len > 2 select new PRC { Protocol = parts[0], Port = int.Parse(parts[1].Split(':').Last()), PID = int.Parse(parts[len - 1]) }).ToList());
    }
}