last ...
This commit is contained in:
@@ -1,168 +0,0 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Attributes;
|
||||
using xExceptions.Constants;
|
||||
|
||||
namespace xAiApi.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Several OS Type ...
|
||||
/// </summary>
|
||||
public enum XOSType
|
||||
{
|
||||
[StringValue("Unknown")]
|
||||
XUnknown,
|
||||
|
||||
[StringValue("Mac")]
|
||||
XMacOs,
|
||||
|
||||
[StringValue("Linux")]
|
||||
XLinux,
|
||||
|
||||
[StringValue("Windows")]
|
||||
XWindows,
|
||||
|
||||
[StringValue("FreeBSD")]
|
||||
XFreeBSD,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OS Command Execution Result ...
|
||||
/// </summary>
|
||||
public class XOSCMDResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Execution Result ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string Result { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Execution Error ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string Error { get; set; }
|
||||
}
|
||||
|
||||
public static class XOsHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieve OS Type ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static XOSType GetOSType()
|
||||
{
|
||||
//
|
||||
XOSType result = XOSType.XUnknown;
|
||||
|
||||
//
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
result = XOSType.XMacOs;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
result = XOSType.XLinux;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
result = XOSType.XWindows;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
|
||||
{
|
||||
result = XOSType.XFreeBSD;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve OS Description ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetOSDescription()
|
||||
{
|
||||
//
|
||||
var result = RuntimeInformation.OSDescription;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute an Specified Command ...
|
||||
/// </summary>
|
||||
/// <param name="cmd">Specified Command</param>
|
||||
/// <param name="args">Command Arguments</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<XOSCMDResult> Execute(
|
||||
string cmd,
|
||||
string args
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = new XOSCMDResult
|
||||
{
|
||||
Error = "Unknown",
|
||||
};
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate CMD ...
|
||||
var isValid = !cmd.IsNullOrEmpty();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Validate OS Type ...
|
||||
XOSType osType = GetOSType();
|
||||
isValid = osType != XOSType.XUnknown;
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
ProcessStartInfo psi = new ProcessStartInfo();
|
||||
|
||||
//
|
||||
psi.FileName = cmd;
|
||||
psi.Arguments = args;
|
||||
psi.UseShellExecute = false;
|
||||
psi.RedirectStandardError = true;
|
||||
psi.RedirectStandardOutput = true;
|
||||
|
||||
//
|
||||
Process process = Process.Start(psi);
|
||||
|
||||
//
|
||||
// Read the output
|
||||
string output = process.StandardOutput.ReadToEnd();
|
||||
string error = process.StandardError.ReadToEnd();
|
||||
|
||||
//
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
//
|
||||
result = new XOSCMDResult
|
||||
{
|
||||
Error = error,
|
||||
Result = output,
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Error = ex.Message;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user