From d434e418b3150d71b4053931d1a7d1521ee51860 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 19 May 2020 21:05:38 +0000 Subject: [PATCH] Backup --- server/AyaNova/util/RunProgram.cs | 125 ++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 server/AyaNova/util/RunProgram.cs diff --git a/server/AyaNova/util/RunProgram.cs b/server/AyaNova/util/RunProgram.cs new file mode 100644 index 00000000..77e37cfc --- /dev/null +++ b/server/AyaNova/util/RunProgram.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System; +using System.Diagnostics; + +using System.Runtime.InteropServices; + +namespace AyaNova.Util +{ + public static class RunProgram + { + + + public static string Run(string cmd, string arguments) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return RunWindows(cmd, arguments); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return RunOSX(cmd, arguments); + + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return RunLinuxBash(cmd, arguments); + } + else + { + throw new PlatformNotSupportedException(); + } + } + + + // using (var process = new Process()) + // { + // process.StartInfo.FileName = @"..\HelloWorld\bin\Debug\helloworld.exe"; // relative path. absolute path works too. + // process.StartInfo.Arguments = $"{id}"; + // //process.StartInfo.FileName = @"cmd.exe"; + // //process.StartInfo.Arguments = @"/c dir"; // print the current working directory information + // process.StartInfo.CreateNoWindow = true; + // process.StartInfo.UseShellExecute = false; + // process.StartInfo.RedirectStandardOutput = true; + // process.StartInfo.RedirectStandardError = true; + + // process.OutputDataReceived += (sender, data) => Console.WriteLine(data.Data); + // process.ErrorDataReceived += (sender, data) => Console.WriteLine(data.Data); + // Console.WriteLine("starting"); + // process.Start(); + // process.BeginOutputReadLine(); + // process.BeginErrorReadLine(); + // var exited = process.WaitForExit(1000 * 10); // (optional) wait up to 10 seconds + // Console.WriteLine($"exit {exited}"); + // } + + + public static string RunWindows(string cmd, string arguments) + { + + using (var process = new Process()) + { + process.StartInfo = new ProcessStartInfo + { + FileName = cmd, + Arguments = arguments, + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + + process.Start(); + string result = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return result; + } + } + + public static string RunLinuxBash(string cmd, string arguments) + { + var escapedArgs = $"{cmd} {arguments}".Replace("\"", "\\\""); + + using (var process = new Process()) + { + 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(); + return result; + } + } + + + public static string RunOSX(string cmd, string arguments) + { + using (var process = new Process()) + { + process.StartInfo = new ProcessStartInfo + { + FileName = cmd, + Arguments = arguments, + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + + process.Start(); + string result = process.StandardOutput.ReadToEnd(); + process.WaitForExit(); + return result; + } + } + + } +} \ No newline at end of file