You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
6.5 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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);
}
/// <summary>
/// 启动WebApi程序
/// </summary>
/// <param name="exePathAndName">带相对或绝对路径的可执行文件</param>
/// <param name="args">应用程序参数</param>
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);
}
}
/// <summary>
/// 运行WebApi发布程序
/// </summary>
/// <returns></returns>
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;
}
}
}