Для Linux:
using System;
using System.Diagnostics;
using System.IO;
namespace Test
{
class MainClass
{
public static void Main (string[] args)
{
string result = LinuxTerminal.GetOutput("find /sys/class/input -maxdepth 1 -name \"mouse*\"|wc -l");
int outputValue = int.Parse (result);
Console.WriteLine (outputValue);
Console.ReadKey ();
}
}
}
using System;
using System.Diagnostics;
namespace Test
{
public static class LinuxTerminal
{
private const string TerminalPath = "/bin/bash";
public static void ExecuteCommand(string command)
{
var proc = new Process();
proc.StartInfo.FileName = TerminalPath;
proc.StartInfo.Arguments = "-c \" " + command + " \"";
proc.StartInfo.UseShellExecute = false;
proc.Start();
}
public static int GetExitCode(string command)
{
var proc = new Process();
proc.StartInfo.FileName = TerminalPath;
proc.StartInfo.Arguments = "-c \" " + command + " \"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.WaitForExit();
return proc.ExitCode;
}
public static string GetOutput(string command)
{
var proc = new Process();
proc.StartInfo.FileName = TerminalPath;
proc.StartInfo.Arguments = "-c \" " + command + " \"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.WaitForExit();
return proc.StandardOutput.ReadToEnd();
}
}
}
Из под винды так работает запуск программы и получение ExitCode
using System.Diagnostics;
namespace ProcessStart
{
public static class CommandLine
{
public static int ExecuteCommand(string applicationPath, string command = "")
{
var proc = new Process();
proc.StartInfo.FileName = applicationPath;
proc.StartInfo.Arguments = command;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
return proc.ExitCode;
}
}
}
using System;
using System.IO;
namespace ProcessStart
{
class Program
{
private static Program _program;
static void Main(string[] args)
{
_program = new Program();
_program.Run();
}
private void Run()
{
const string ProgramName = "ReturnRandomNumber.exe";
string appName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ProgramName);
int exitCode = CommandLine.ExecuteCommand(appName);
Console.WriteLine(exitCode.ToString());
Console.ReadKey();
}
}
}