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.

154 lines
4.6 KiB
C#

9 months ago
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,
8 months ago
//StandardOutputEncoding = Encoding.UTF8,
9 months ago
RedirectStandardError = true,
8 months ago
//StandardErrorEncoding = Encoding.UTF8,
9 months ago
RedirectStandardInput = false,
8 months ago
//StandardInputEncoding = Encoding.UTF8,
9 months ago
UseShellExecute = false,
CreateNoWindow = false,
};
Process process = new Process()
{
StartInfo = startInfo,
};
process.Start();
9 months ago
if (startInfo.RedirectStandardInput)
{
process.StandardInput.Write("");
}
9 months ago
output = process.StandardOutput.ReadToEnd();
if (waitForExit)
{
process.WaitForExit();
}
}
catch (Exception ex)
{
output = $"An error occurred: {ex.Message}";
}
return output;
}
8 months ago
/// <summary>
/// 新窗口启动WebApp项目
/// </summary>
/// <param name="startFile">包含全路径的WebApp项目</param>
8 months ago
/// <param name="args">命令参数</param>
8 months ago
/// <returns></returns>
8 months ago
public static string RunWebApp(string startFile, params string[] args)
8 months ago
{
string output = "";
try
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
8 months ago
Arguments = $"Start-Process -FilePath '{startFile}' -ArgumentList '{string.Join(" ", args)}' ",
UseShellExecute = true,
8 months ago
CreateNoWindow = false,
};
Process process = new Process()
{
StartInfo = startInfo,
};
process.Start();
8 months ago
output = "已在新窗口中启动WebApp项目。如果已有实例在运行或者执行异常则窗口会自动关闭否则新窗口一直存在,直到手动关闭!";
8 months ago
}
catch (Exception ex)
{
output = $"An error occurred: {ex.Message}";
}
return output;
}
/// <summary>
/// 启动新窗口,执行指定文件
/// </summary>
/// <param name="startFile">包含全路径的可执行文件</param>
/// <returns></returns>
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;
}
9 months ago
}
}