178 lines
4.5 KiB
C#
178 lines
4.5 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Attributes;
|
|
using xExceptions.Constants;
|
|
|
|
namespace xCommons.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; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// a Helper Class which Provides OS Command Executions ...
|
|
/// </summary>
|
|
public static partial 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();
|
|
}
|
|
|
|
//
|
|
return await Task.Run<XOSCMDResult>(() =>
|
|
{
|
|
//
|
|
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();
|
|
|
|
//
|
|
process.WaitForExit();
|
|
|
|
//
|
|
result = new XOSCMDResult
|
|
{
|
|
Error = error,
|
|
Result = output,
|
|
};
|
|
|
|
//
|
|
return result;
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Error = ex.Message;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |