B
B
Boris the Animal2016-10-11 14:26:29
linux
Boris the Animal, 2016-10-11 14:26:29

How can I get the Exit Code by running a program from the command line?

Returns nothing. I'm sitting under Windows, but in general I need to return the result from under Linux.
In general, the program hangs in anticipation of the completion of the command line forever.

using System.Diagnostics;

namespace ProcessStart
{
    public static class CommandLine
    {
        public static int ExecuteCommand(string command)
        {
            var proc = new Process();
            proc.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe ";
            proc.StartInfo.Arguments = command;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            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); // "ECHO time");
            Console.WriteLine(exitCode.ToString());

            Console.ReadKey();
        }
    }
}

For Linux:
using System.Diagnostics;

namespace ProcessStart
{
    public static class LinuxTerminal
    {
        public static void ExecuteCommand(string command)
        {
            var proc = new Process();
            proc.StartInfo.FileName = "/bin/bash";
            proc.StartInfo.Arguments = "-c \" " + command + " \"";
            proc.StartInfo.UseShellExecute = false;
            proc.Start();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris the Animal, 2016-10-11
@Casper-SC

For 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();
    }
  }
}

From under Windows, this is how running the program and getting the ExitCode works
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();
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question