using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HttpClientStudy.Core.Utilities
{
///
/// dotnet命令行工具类
///
public static class DotnetCommondUtility
{
///
/// 执行dotnet命令
///
/// 命令
/// 命令参数
/// 新窗口执行命令
/// 命令行输出文本
public static string ExecuteCommand(string command, string argument = "", bool newWindow = false)
{
string output = "";
try
{
if (newWindow)
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "pwsh.exe",
Arguments = $" Start-Process -FilePath 'dotnet' {command} {argument}",
//RedirectStandardOutput = true,
//StandardOutputEncoding = Encoding.UTF8,
//RedirectStandardError = true,
//StandardErrorEncoding = Encoding.UTF8,
//RedirectStandardInput = false,
//StandardInputEncoding = Encoding.UTF8,
UseShellExecute = false,
CreateNoWindow = false,
};
Process process = new Process()
{
StartInfo = startInfo,
};
process.Start();
output = "命令已在新窗口执行!";
}
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,
//启用内核模式时,不能重定向输出
UseShellExecute = false,
//不创建新窗口
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();
}
}
catch (Exception ex)
{
output = $"An error occurred: {ex.Message}";
}
return output;
}
///
/// 新窗口启动WebApp项目
///
/// 包含全路径的WebApp项目
/// 命令参数
///
public static string RunWebApp(string startFile, params string[] args)
{
string output = "";
try
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"Start-Process -FilePath '{startFile}' -ArgumentList '{string.Join(" ", args)}' ",
UseShellExecute = true,
CreateNoWindow = false,
};
Process process = new Process()
{
StartInfo = startInfo,
};
process.Start();
output = "已在新窗口中启动WebApp项目。如果已有实例在运行或者执行异常,则窗口会自动关闭;否则新窗口一直存在,直到手动关闭!";
}
catch (Exception ex)
{
output = $"An error occurred: {ex.Message}";
}
return output;
}
///
/// 停止WebAPI项目
///
/// 包含全路径的可执行文件
///
public static string SopWebApp()
{
string output = "";
try
{
string projectAndMutexName = WebApiConfigManager.GetWebApiConfigOption().CurrentValue.WebAppMutexName;
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"Stop-Process -Name '{projectAndMutexName}'",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
StandardOutputEncoding = Encoding.UTF8,
};
Process process = new Process()
{
StartInfo = startInfo,
};
process.Start();
process.WaitForExit();
output = process.StandardOutput.ReadToEnd();
}
catch (Exception ex)
{
output = $"An error occurred: {ex.Message}";
}
return output;
}
}
}