From 8269948288db7da74e1098c50bbcec3aef224387 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Tue, 23 Mar 2021 16:14:34 +0800 Subject: [PATCH] Update GetProcessByUsedTcpPort (Fix #591) --- Netch/Controllers/MainController.cs | 12 +++++++++++- Netch/Utils/PortHelper.cs | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index c28969a1..305aa42d 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -193,7 +193,17 @@ namespace Netch.Controllers { foreach (var p in PortHelper.GetProcessByUsedTcpPort(port)) { - if (p.MainModule!.FileName.StartsWith(Global.NetchDir)) + try + { + _ = p.MainModule!.FileName; + } + catch (Exception e) + { + Logging.Warning(e.ToString()); + continue; + } + + if (p.MainModule.FileName.StartsWith(Global.NetchDir)) { p.Kill(); p.WaitForExit(); diff --git a/Netch/Utils/PortHelper.cs b/Netch/Utils/PortHelper.cs index 459ecd88..27df68cc 100644 --- a/Netch/Utils/PortHelper.cs +++ b/Netch/Utils/PortHelper.cs @@ -33,7 +33,7 @@ namespace Netch.Utils if (port == 0) throw new ArgumentOutOfRangeException(); - var row = GetTcpTable2().Where(r => ntohs((ushort) r.dwLocalPort) == port); + var row = GetTcpTable2().Where(r => ntohs((ushort) r.dwLocalPort) == port).Where(r => r.dwOwningPid is not (0 or 4)); return row.Select(r => Process.GetProcessById((int) r.dwOwningPid)); }