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()