From 885f70fb567efb6a1559b4fe19d52f464eea0a96 Mon Sep 17 00:00:00 2001 From: "MSI\\letre" Date: Thu, 10 Jul 2025 16:34:35 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91=20?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=9C=AC=E5=9C=B0=E5=88=A4=E5=88=86=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=89=8D=EF=BC=8C=E5=85=88=E6=A3=80=E6=B5=8B=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=B7=B2=E7=BB=8F=E5=90=AF=E5=8A=A8=EF=BC=8C=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E5=88=99=E4=B8=8D=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Applications/Program.cs | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/Applications/Program.cs b/Applications/Program.cs index 05d7e06..2aa2dbf 100644 --- a/Applications/Program.cs +++ b/Applications/Program.cs @@ -1,17 +1,18 @@ -using Applications; -using Microsoft.Win32; +using System; using System.Collections.Concurrent; -using System.Diagnostics; -using System.Net; -using System.Security.Cryptography; -using System.Text.Json; -using System.Xml.Linq; -using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; -using MySql.Data.MySqlClient; +using System.Net; +using System.Net.NetworkInformation; +using System.Security.Cryptography; using System.Security.Principal; using System.ServiceProcess; +using System.Text.Json; +using System.Xml.Linq; +using Applications; +using Microsoft.Win32; +using MySql.Data.MySqlClient; class Program { @@ -70,7 +71,12 @@ class Program WorkingDirectory = workingDirectory, // 必须! UseShellExecute = true // 或 false,根据需要 }; - Process.Start(startInfo); + // 检测一下端口是否被占用48080 + if (!IsPortInUse(48082)) + { + Process.Start(startInfo); + } + } } else if (action == "/start") @@ -510,6 +516,21 @@ class Program list.Add(sql.Substring(start)); return list; } + public static bool IsPortInUse(int port) + { + IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); + TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); + + foreach (TcpConnectionInformation tcpi in tcpConnInfoArray) + { + if (tcpi.LocalEndPoint.Port == port) + { + return true; // 端口被占用 + } + } + + return false; // 端口可用 + } // 获取当前登录用户 SID static string GetCurrentUserSid()