-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLI.cs
50 lines (47 loc) · 1.99 KB
/
CLI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
namespace LivingThing.LiveBlazor
{
public static class CLIExtension
{
public static Task<(int ExitCode, string StdOut)> CLI(this string command, bool runDirect = false)
{
return Task.Run(() =>
{
Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/c " + command;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
var response = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return (process.ExitCode, response);
////Console.WriteLine($"Executing \"{command}\"");
//Process cmd = new Process();
//cmd.StartInfo.FileName = !runDirect ? "cmd.exe" : command;
//cmd.StartInfo.RedirectStandardInput = true;
//cmd.StartInfo.RedirectStandardOutput = true;
//cmd.StartInfo.CreateNoWindow = true;
//cmd.StartInfo.UseShellExecute = false;
//cmd.Start();
//if (!runDirect)
//{
// cmd.StandardInput.WriteLine(command);
//}
//cmd.StandardInput.Flush();
//cmd.StandardInput.Close();
//cmd.WaitForExit();
//string response = cmd.StandardOutput.ReadToEnd();
////Console.WriteLine($"Executed with code \"{cmd.ExitCode}\" -> \"{response}\"");
//return (cmd.ExitCode, response);
});
}
}
}