2
0
Files
controlmymonitormanagement/Library/Helpers/ConsoleHelper.cs

104 lines
3.1 KiB
C#
Raw Normal View History

2023-07-03 01:51:09 +08:00
using System.Diagnostics;
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
namespace CMM.Library.Helpers;
internal class ConsoleHelper
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
const string cmdFileName = "cmd.exe";
private static Process CreatProcess(string fileName) =>
new Process()
{
StartInfo = new ProcessStartInfo
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
FileName = fileName,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
}
};
public static async Task<string> ExecuteCommand(string command, int timeoutMs = 5000)
2023-07-03 01:51:09 +08:00
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = command;
p.Start();
using var cts = new CancellationTokenSource(timeoutMs);
try
{
var output = await p.StandardOutput.ReadToEndAsync(cts.Token);
await p.WaitForExitAsync(cts.Token);
return output;
}
catch (OperationCanceledException)
{
try { p.Kill(); } catch { }
return string.Empty;
}
2023-07-03 01:51:09 +08:00
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static async Task<string> CmdCommandAsync(params string[] cmds) =>
await CommandAsync(cmdFileName, cmds);
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static string CmdCommand(params string[] cmds) =>
Command(cmdFileName, cmds);
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static async Task<string> CommandAsync(string fileName, params string[] cmds)
{
return await CommandAsync(fileName, 5000, cmds);
}
public static async Task<string> CommandAsync(string fileName, int timeoutMs, params string[] cmds)
2023-07-03 01:51:09 +08:00
{
var p = CreatProcess(fileName);
p.Start();
foreach (var cmd in cmds)
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
p.StandardInput.WriteLine(cmd);
2022-05-23 00:58:58 +08:00
}
2023-07-03 01:51:09 +08:00
p.StandardInput.WriteLine("exit");
using var cts = new CancellationTokenSource(timeoutMs);
try
{
var result = await p.StandardOutput.ReadToEndAsync(cts.Token);
var error = await p.StandardError.ReadToEndAsync(cts.Token);
if (!string.IsNullOrWhiteSpace(error))
result = result + "\r\n<Error Message>:\r\n" + error;
await p.WaitForExitAsync(cts.Token);
p.Close();
Debug.WriteLine(result);
return result;
}
catch (OperationCanceledException)
{
try { p.Kill(); } catch { }
return string.Empty;
}
2023-07-03 01:51:09 +08:00
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
public static string Command(string fileName, params string[] cmds)
{
var p = CreatProcess(fileName);
p.Start();
foreach (var cmd in cmds)
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
p.StandardInput.WriteLine(cmd);
2022-05-23 00:58:58 +08:00
}
2023-07-03 01:51:09 +08:00
p.StandardInput.WriteLine("exit");
var result = p.StandardOutput.ReadToEnd();
var error = p.StandardError.ReadToEnd();
if (!string.IsNullOrWhiteSpace(error))
result = result + "\r\n<Error Message>:\r\n" + error;
p.WaitForExit();
p.Close();
Debug.WriteLine(result);
return result;
2022-05-23 00:58:58 +08:00
}
}