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.

189 lines
6.0 KiB
C#

11 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>
10 months ago
/// <param name="argument">命令参数</param>
/// <param name="newWindow">新窗口执行命令</param>
11 months ago
/// <returns>命令行输出文本</returns>
10 months ago
public static string ExecuteCommand(string command, string argument = "", bool newWindow = false)
11 months ago
{
string output = "";
try
{
10 months ago
if (newWindow)
11 months ago
{
10 months ago
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 = "命令已在新窗口执行!";
11 months ago
}
10 months ago
else
11 months ago
{
10 months ago
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();
//等待里程结束
11 months ago
process.WaitForExit();
}
}
catch (Exception ex)
{
output = $"An error occurred: {ex.Message}";
}
return output;
}
11 months ago
/// <summary>
/// 新窗口启动WebApp项目
/// </summary>
/// <param name="startFile">包含全路径的WebApp项目</param>
10 months ago
/// <param name="args">命令参数</param>
11 months ago
/// <returns></returns>
10 months ago
public static string RunWebApp(string startFile, params string[] args)
11 months ago
{
string output = "";
try
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
10 months ago
Arguments = $"Start-Process -FilePath '{startFile}' -ArgumentList '{string.Join(" ", args)}' ",
UseShellExecute = true,
11 months ago
CreateNoWindow = false,
};
Process process = new Process()
{
StartInfo = startInfo,
};
process.Start();
10 months ago
output = "已在新窗口中启动WebApp项目。如果已有实例在运行或者执行异常则窗口会自动关闭否则新窗口一直存在,直到手动关闭!";
11 months ago
}
catch (Exception ex)
{
output = $"An error occurred: {ex.Message}";
}
return output;
}
/// <summary>
10 months ago
/// 停止WebAPI项目
11 months ago
/// </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;
}
11 months ago
}
}