From 3783a98b929722028f8cd8f0e30af212261d8880 Mon Sep 17 00:00:00 2001 From: "MSI\\letre" Date: Mon, 14 Jul 2025 09:57:52 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=9C=AC=E5=9C=B0=E5=88=A4=E5=88=86=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=B7=B2=E7=BB=8F=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Applications/Program.cs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/Applications/Program.cs b/Applications/Program.cs index 2aa2dbf..2b274e8 100644 --- a/Applications/Program.cs +++ b/Applications/Program.cs @@ -72,7 +72,7 @@ class Program UseShellExecute = true // 或 false,根据需要 }; // 检测一下端口是否被占用48080 - if (!IsPortInUse(48082)) + if (!IsPortInUse("judgement")) { Process.Start(startInfo); } @@ -516,19 +516,29 @@ class Program list.Add(sql.Substring(start)); return list; } - public static bool IsPortInUse(int port) + public static bool IsPortInUse(string name) { - IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); - TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); - - foreach (TcpConnectionInformation tcpi in tcpConnInfoArray) + foreach (var proc in Process.GetProcesses()) { - if (tcpi.LocalEndPoint.Port == port) + try { - return true; // 端口被占用 + // 主窗口句柄不为 0,表示是一个窗口程序(用户级应用) + if (proc.SessionId != 0 && + proc.Responding) + { + if (proc.ProcessName.Contains(name)) + { + var procs = Process.GetProcessById(proc.Id); + return true; + } + } + } + + catch + { + // 一些系统进程可能抛异常,忽略 } } - return false; // 端口可用 }