diff --git a/HttpClientStudy.Core/Utilities/AppUtility.cs b/HttpClientStudy.Core/Utilities/AppUtility.cs index a28d686..64241a5 100644 --- a/HttpClientStudy.Core/Utilities/AppUtility.cs +++ b/HttpClientStudy.Core/Utilities/AppUtility.cs @@ -116,10 +116,13 @@ namespace HttpClientStudy.Core.Utilities } /// - /// 运行WebApi发布程序 + /// 新窗口运行WebApi发布程序 /// + /// WebApi程序文件 + /// WebApi启动参数 + /// 是否保持命令窗口(不关闭) /// - public static string RunWebApiExeFile(string exeFile, bool stayWindows = false, params string[] args) + public static string RunWebApiExeFile(string exeFile, bool stayWindow = false, params string[] args) { string executedMessage = string.Empty; try @@ -135,29 +138,39 @@ namespace HttpClientStudy.Core.Utilities return executedMessage; } + //纯文件名(不带扩展名)作用 新窗口标题 string fileName = Path.GetFileNameWithoutExtension(exeFile); - string stayWindowsArg = stayWindows ? "/k" : "/c"; + //不同操作系统,需要不同的启动命令 string systemShell = "cmd.exe"; - //string webApiArgs = $"{stayWindowsArg} start \"{fileName}\" \"{exeFile}\" {string.Join(" ", args)}"; - //或者 - string webApiArgs = $"{stayWindowsArg} start \"{fileName}\" \"\"\"{exeFile}\" {string.Join(" ", args)}"; + string cmdArgsText = string.Empty; + + //保持新命令窗口(不自动关闭) + if (stayWindow) + { + cmdArgsText = $"/c start \"{fileName}\" cmd /k \"\"\"{exeFile}\" {string.Join(" ",args)}"; + } + else + { + cmdArgsText = $"/c start \"{fileName}\" \"\"\"{exeFile}\" {string.Join(" ",args)}"; + } ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = systemShell, - Arguments = webApiArgs, + Arguments = cmdArgsText, - //未知原因:只有 UseShellExecute 设置为 true 时,CreateNoWindow参数才有效,新窗口执行才实际有效。 - UseShellExecute = true, + //执行命令的线程本身不弹出新窗口 + //确保弹出新窗口:是使用cmd start 实现 + //保持新窗口不关闭:使用 start cmd /k 实现 + UseShellExecute = false, //true时不创建新窗口,false才是创建新窗口 CreateNoWindow = false, }; - //启动进程 using (Process process = new Process() { StartInfo = startInfo }) { @@ -209,40 +222,6 @@ namespace HttpClientStudy.Core.Utilities return executedMessage; } - /// - /// 互斥锁是否存在 - /// - /// 互斥锁名称 - /// 是否存在 - public static bool WebAppIsRunningByMutex(string mutexName) - { - bool createdResult = true; - - //创建互斥锁 - using (var mutex = new Mutex(true, mutexName, out createdResult)) - { - if (createdResult) - { - mutex.ReleaseMutex(); - } - } - - //互斥锁是否创建成功 - return !createdResult; - } - - /// - /// 进程是否存在 - /// - /// 进程名 - /// 是否存在 - public static bool WebAppIsRunningByProcessName(string processName) - { - bool processExists = Process.GetProcessesByName(processName).ToList().Count == 0; - - return processExists; - } - /// /// 执行CMD命令 /// @@ -292,5 +271,39 @@ namespace HttpClientStudy.Core.Utilities return executedMessage; } + /// + /// 互斥锁是否存在 + /// + /// 互斥锁名称 + /// 是否存在 + private static bool WebAppIsRunningByMutex(string mutexName) + { + bool createdResult = true; + + //创建互斥锁 + using (var mutex = new Mutex(true, mutexName, out createdResult)) + { + if (createdResult) + { + mutex.ReleaseMutex(); + } + } + + //互斥锁是否创建成功 + return !createdResult; + } + + /// + /// 进程是否存在 + /// + /// 进程名 + /// 是否存在 + private static bool WebAppIsRunningByProcessName(string processName) + { + bool processExists = Process.GetProcessesByName(processName).ToList().Count == 0; + + return processExists; + } + } } diff --git a/HttpClientStudy.UnitTest/UtilitiesTest/AppUtilityTest.cs b/HttpClientStudy.UnitTest/UtilitiesTest/AppUtilityTest.cs index f70a69e..e17d4dd 100644 --- a/HttpClientStudy.UnitTest/UtilitiesTest/AppUtilityTest.cs +++ b/HttpClientStudy.UnitTest/UtilitiesTest/AppUtilityTest.cs @@ -30,6 +30,32 @@ namespace HttpClientStudy.UnitTest.UtilitiesTest _output.WriteLine(message); } + [Fact] + public void RunWebApiExeFile_Test() + { + string message = string.Empty; + + string apiFile = Path.GetFullPath("../../../../Docs/Publish/HttpClientStudy.WebApp/HttpClientStudy.WebApp.exe", Environment.CurrentDirectory); + + _output.WriteLine($"文件:{apiFile}"); + + message = AppUtility.RunWebApiExeFile(apiFile); + _output.WriteLine(message); + } + + [Fact] + public void RunWebApiExeFile_StayWindow_Test() + { + string message = string.Empty; + + string apiFile = Path.GetFullPath("../../../../Docs/Publish/HttpClientStudy.WebApp/HttpClientStudy.WebApp.exe", Environment.CurrentDirectory); + + _output.WriteLine($"文件:{apiFile}"); + + message = AppUtility.RunWebApiExeFile(apiFile,true, "--urls=http://localhost:8644"); + _output.WriteLine(message); + } + [Fact] public void Cmd_Test() {