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; // 端口可用 }