namespace ExecuteCommandStudy.Core { public static class Utils1 { static void A() { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/u /k dotnet exec c:\\Users\\ruyu\\Desktop\\HttpClientStudy\\Docs\\Publish\\HttpClientStudy.WebApp\\HttpClientStudy.WebApp.dll"; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = false; Process.Start(startInfo); } /// /// 启动WebApi程序 /// /// 带相对或绝对路径的可执行文件 /// 应用程序参数 static string B(string exePathAndName, params string[] args) { string executedMessage = string.Empty; try { if (!Path.IsPathRooted(exePathAndName)) { exePathAndName = Path.GetFullPath(exePathAndName, Environment.CurrentDirectory); } if (!File.Exists(exePathAndName)) { executedMessage = $"可执行文件[{exePathAndName}]不存在"; return executedMessage; } ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = exePathAndName; startInfo.Arguments = string.Join(" ", args); //未知原因:只有 UseShellExecute 设置为 true 时,CreateNoWindow参数才有效,新窗口执行才实际有效。 startInfo.UseShellExecute = true; //true时不创建新窗口,false才是创建新窗口 startInfo.CreateNoWindow = false; //启动进程 using (Process process = new Process() { StartInfo = startInfo }) { process.Start(); } executedMessage = $"程序[{exePathAndName}]已在新的命令行窗口执行。如果未出现新命令行窗口,可能是程序错误造成窗口闪现!"; } catch (Exception ex) { executedMessage = $"启动程序[{exePathAndName}]出现异常:[{ex.Message}]"; } finally { } return executedMessage; } static void PowerShell() { // 创建新的进程开始信息 ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "powershell.exe", Arguments = $"Start-Process powershell -ArgumentList '-noexit','-command','dotnet c:\\Users\\ruyu\\Desktop\\HttpClientStudy\\Docs\\Publish\\HttpClientStudy.WebApp\\HttpClientStudy.WebApp.dll'", UseShellExecute = false, RedirectStandardOutput = false, CreateNoWindow = false // 设置为false以在新窗口中打开 }; try { // 使用启动信息创建并启动进程 using (Process process = new Process() { StartInfo = startInfo }) { process.Start(); } } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } } static void RunExe() { // PowerShell(); var webapiExe = "c:\\Users\\ruyu\\Desktop\\HttpClientStudy\\Docs\\Publish\\HttpClientStudy.WebApp\\HttpClientStudy.WebApp.exe"; // 创建新的进程开始信息 ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "powershell.exe", Arguments = $"Start-Process {webapiExe} -ArgumentList '-noexit'", UseShellExecute = false, RedirectStandardOutput = false, CreateNoWindow = false // 设置为false以在新窗口中打开 }; try { // 使用启动信息创建并启动进程 using (Process process = new Process() { StartInfo = startInfo }) { process.Start(); } } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } } /// /// 运行WebApi发布程序 /// /// public static string RunWebApiExeFile(string exeFile, bool stayWindows = false, params string[] args) { string executedMessage = string.Empty; try { if (!Path.IsPathRooted(exeFile)) { exeFile = Path.GetFullPath(exeFile, Environment.CurrentDirectory); } if (!File.Exists(exeFile)) { executedMessage = $"可执行文件[{exeFile}]不存在"; 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)}"; ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = systemShell, Arguments = webApiArgs, //未知原因:只有 UseShellExecute 设置为 true 时,CreateNoWindow参数才有效,新窗口执行才实际有效。 UseShellExecute = true, //true时不创建新窗口,false才是创建新窗口 CreateNoWindow = false, }; //启动进程 using (Process process = new Process() { StartInfo = startInfo }) { process.Start(); } executedMessage = $"程序[{exeFile}]已在新的命令行窗口执行。如果未出现新命令行窗口,可能是程序错误造成窗口闪现!"; } catch (Exception ex) { executedMessage = $"启动程序[{exeFile}]出现异常:[{ex.Message}]"; } finally { } return executedMessage; } } }