如何在 C# 中终止使用 TCP 端口的应用程序?

问题描述 投票:0回答:2

我想在我的应用程序启动期间释放一个TCP端口(要求用户确认),如何获取PID号,然后,如果用户确认,杀死它?

我知道我可以通过 netstat 获取此信息,但如何在脚本中或更好地使用 C# 方法来获取此信息。

c# tcp kill-process netstat
2个回答
18
投票

您可以运行 netstat,然后将输出重定向到文本流,以便您可以解析并获取所需的信息。

这就是我所做的。

  • netstat -a -n -o
    作为进程运行
  • 重定向标准输出并捕获输出文本
  • 捕获结果,解析并返回所有正在使用的进程
  • 检查端口是否正在使用
  • 使用
    linq
  • 查找进程
  • 奔跑
    Process.Kill()

您必须进行异常处理。

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
投票

@yohannist 答案的改进版本(缺少

using
、不必要的 GUI 窗口和其他一些东西)。以下代码可以用作:

ProcMgr.KillWherePort(port)

实施:

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());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.