|
|
@ -16,24 +16,26 @@ namespace HttpClientStudy.Core.Utilities
|
|
|
|
/// 执行dotnet命令
|
|
|
|
/// 执行dotnet命令
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="command">命令</param>
|
|
|
|
/// <param name="command">命令</param>
|
|
|
|
/// <param name="arguments">命令参数</param>
|
|
|
|
/// <param name="argument">命令参数</param>
|
|
|
|
/// <param name="waitForExit">是否等待退出</param>
|
|
|
|
/// <param name="newWindow">新窗口执行命令</param>
|
|
|
|
/// <returns>命令行输出文本</returns>
|
|
|
|
/// <returns>命令行输出文本</returns>
|
|
|
|
public static string ExecuteCommand(string command, string arguments = "", bool waitForExit=false)
|
|
|
|
public static string ExecuteCommand(string command, string argument = "", bool newWindow = false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
string output = "";
|
|
|
|
string output = "";
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (newWindow)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo()
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
FileName = "dotnet",
|
|
|
|
FileName = "pwsh.exe",
|
|
|
|
Arguments = $"{command} {arguments}",
|
|
|
|
Arguments = $" Start-Process -FilePath 'dotnet' {command} {argument}",
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
//RedirectStandardOutput = true,
|
|
|
|
//StandardOutputEncoding = Encoding.UTF8,
|
|
|
|
//StandardOutputEncoding = Encoding.UTF8,
|
|
|
|
RedirectStandardError = true,
|
|
|
|
//RedirectStandardError = true,
|
|
|
|
//StandardErrorEncoding = Encoding.UTF8,
|
|
|
|
//StandardErrorEncoding = Encoding.UTF8,
|
|
|
|
RedirectStandardInput = false,
|
|
|
|
//RedirectStandardInput = false,
|
|
|
|
//StandardInputEncoding = Encoding.UTF8,
|
|
|
|
//StandardInputEncoding = Encoding.UTF8,
|
|
|
|
UseShellExecute = false,
|
|
|
|
UseShellExecute = false,
|
|
|
|
CreateNoWindow = false,
|
|
|
|
CreateNoWindow = false,
|
|
|
@ -46,15 +48,49 @@ namespace HttpClientStudy.Core.Utilities
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
process.Start();
|
|
|
|
process.Start();
|
|
|
|
if (startInfo.RedirectStandardInput)
|
|
|
|
|
|
|
|
{
|
|
|
|
output = "命令已在新窗口执行!";
|
|
|
|
process.StandardInput.Write("");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
FileName = "dotnet",
|
|
|
|
|
|
|
|
Arguments = $"{command} {argument}",
|
|
|
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
|
|
|
StandardOutputEncoding = Encoding.UTF8,
|
|
|
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
|
|
|
StandardErrorEncoding = Encoding.UTF8,
|
|
|
|
|
|
|
|
//RedirectStandardInput = false,
|
|
|
|
|
|
|
|
//StandardInputEncoding = Encoding.UTF8,
|
|
|
|
|
|
|
|
|
|
|
|
output = process.StandardOutput.ReadToEnd();
|
|
|
|
//启用内核模式时,不能重定向输出
|
|
|
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
|
|
|
|
|
|
|
if (waitForExit)
|
|
|
|
//不创建新窗口
|
|
|
|
|
|
|
|
CreateNoWindow = false,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Process process = new Process()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
StartInfo = startInfo,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//处理输出
|
|
|
|
|
|
|
|
process.OutputDataReceived += (sender, args) =>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(args.Data))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
output += args.Data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
process.Start();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//开始异步读取输出
|
|
|
|
|
|
|
|
process.BeginOutputReadLine();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//等待里程结束
|
|
|
|
process.WaitForExit();
|
|
|
|
process.WaitForExit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -66,7 +102,6 @@ namespace HttpClientStudy.Core.Utilities
|
|
|
|
return output;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 新窗口启动WebApp项目
|
|
|
|
/// 新窗口启动WebApp项目
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
@ -107,7 +142,7 @@ namespace HttpClientStudy.Core.Utilities
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 启动新窗口,执行指定文件
|
|
|
|
/// 停止WebAPI项目
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="startFile">包含全路径的可执行文件</param>
|
|
|
|
/// <param name="startFile">包含全路径的可执行文件</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <returns></returns>
|
|
|
|