2017-10-05 21:28:47 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
2017-10-08 23:22:20 +02:00
|
|
|
|
using System.Runtime.InteropServices;
|
2017-10-05 21:28:47 +02:00
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.CLI
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ProcessHelper
|
|
|
|
|
|
{
|
2017-10-06 02:54:55 +02:00
|
|
|
|
public static void CmdExecute(string command, string workingDirectoryPath, bool output = true, bool waitForExit = true)
|
2017-10-05 21:28:47 +02:00
|
|
|
|
{
|
|
|
|
|
|
using (Process cmd = new Process())
|
|
|
|
|
|
{
|
2017-10-08 23:22:20 +02:00
|
|
|
|
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
|
|
|
|
|
|
|
|
|
|
|
if (isWindows)
|
|
|
|
|
|
{
|
|
|
|
|
|
cmd.StartInfo.FileName = "cmd.exe";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-10-11 23:29:58 +02:00
|
|
|
|
// works for OSX and Linux (at least on Ubuntu)
|
2017-10-08 23:22:20 +02:00
|
|
|
|
cmd.StartInfo.FileName = "bash";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-05 21:28:47 +02:00
|
|
|
|
cmd.StartInfo.RedirectStandardInput = true;
|
|
|
|
|
|
cmd.StartInfo.RedirectStandardOutput = true;
|
|
|
|
|
|
cmd.StartInfo.CreateNoWindow = true;
|
|
|
|
|
|
cmd.StartInfo.UseShellExecute = false;
|
|
|
|
|
|
cmd.StartInfo.WorkingDirectory = workingDirectoryPath;
|
|
|
|
|
|
|
|
|
|
|
|
cmd.Start();
|
|
|
|
|
|
|
|
|
|
|
|
cmd.StandardInput.WriteLine(command);
|
|
|
|
|
|
cmd.StandardInput.Flush();
|
|
|
|
|
|
cmd.StandardInput.Close();
|
2017-10-06 02:54:55 +02:00
|
|
|
|
|
|
|
|
|
|
if(waitForExit)
|
|
|
|
|
|
{
|
|
|
|
|
|
cmd.WaitForExit();
|
|
|
|
|
|
}
|
2017-10-05 21:28:47 +02:00
|
|
|
|
|
|
|
|
|
|
if (output)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|