12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Text;
- namespace Infrastructure
- {
- public class ShellHelper
- {
- /// <summary>
- /// linux 系统命令
- /// </summary>
- /// <param name="command"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// windows系统命令
- /// </summary>
- /// <param name="fileName"></param>
- /// <param name="args"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|