56 lines
1.6 KiB
C#
Raw Permalink Normal View History

2024-06-07 11:04:26 +08:00
using System.Diagnostics;
2021-08-23 16:57:25 +08:00
2021-09-25 09:23:50 +08:00
namespace Infrastructure
2021-08-23 16:57:25 +08:00
{
public class ShellHelper
{
2022-01-08 21:46:32 +08:00
/// <summary>
/// linux 系统命令
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
2021-08-23 16:57:25 +08:00
public static string Bash(string command)
{
var escapedArgs = command.Replace("\"", "\\\"");
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Dispose();
return result;
}
2022-01-08 21:46:32 +08:00
/// <summary>
/// windows系统命令
/// </summary>
/// <param name="fileName"></param>
/// <param name="args"></param>
/// <returns></returns>
2021-08-23 16:57:25 +08:00
public static string Cmd(string fileName, string args)
{
string output = string.Empty;
var info = new ProcessStartInfo();
info.FileName = fileName;
info.Arguments = args;
info.RedirectStandardOutput = true;
using (var process = Process.Start(info))
{
output = process.StandardOutput.ReadToEnd();
}
return output;
}
}
}