|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace HttpClientStudy.Core.Utilities
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// dotnet命令行工具类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class DotnetCommondUtility
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行dotnet命令
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="command">命令</param>
|
|
|
|
|
/// <param name="arguments">命令参数</param>
|
|
|
|
|
/// <param name="waitForExit">是否等待退出</param>
|
|
|
|
|
/// <returns>命令行输出文本</returns>
|
|
|
|
|
public static string ExecuteCommand(string command, string arguments = "", bool waitForExit=false)
|
|
|
|
|
{
|
|
|
|
|
string output = "";
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = "dotnet",
|
|
|
|
|
Arguments = $"{command} {arguments}",
|
|
|
|
|
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();
|
|
|
|
|
if (startInfo.RedirectStandardInput)
|
|
|
|
|
{
|
|
|
|
|
process.StandardInput.Write("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output = process.StandardOutput.ReadToEnd();
|
|
|
|
|
|
|
|
|
|
if (waitForExit)
|
|
|
|
|
{
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
output = $"An error occurred: {ex.Message}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|