Files
Electron.NET/ElectronNET.CLI/ProcessHelper.cs

50 lines
1.4 KiB
C#
Raw Normal View History

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
{
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();
if(waitForExit)
{
cmd.WaitForExit();
}
2017-10-05 21:28:47 +02:00
if (output)
{
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
}
}
}
}
}