Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapper for a Command Line Tool in C#

Using MSDN I got the class to write a wrapper for my command line tool.

I now am facing a problem, if I execute the exe through the command line with arguments, it works perfect without any errors.

But when I try to pass the arguments from the Wrapper it crashes the program.

Wanted to know if I am passing the arguments properly and if I am wrong, could somebody point out please. This is the LaunchEXE class from MSDN

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace SPDB
{
    /// <summary>
    /// Class to run any external command line tool with arguments
    /// </summary>
    public class LaunchEXE
    {
        internal static string Run(string exeName, string argsLine, int timeoutSeconds)
        {
            StreamReader outputStream = StreamReader.Null;
            string output = "";
            bool success = false;

            try
            {
                Process newProcess = new Process();
                newProcess.StartInfo.FileName = exeName;
                newProcess.StartInfo.Arguments = argsLine;
                newProcess.StartInfo.UseShellExecute = false;
                newProcess.StartInfo.CreateNoWindow = true; //The command line is supressed to keep the process in the background
                newProcess.StartInfo.RedirectStandardOutput = true;
                newProcess.Start();
                if (0 == timeoutSeconds)
                {
                    outputStream = newProcess.StandardOutput;
                    output = outputStream.ReadToEnd();
                    newProcess.WaitForExit();
                }
                else
                {
                    success = newProcess.WaitForExit(timeoutSeconds * 1000);

                    if (success)
                    {
                        outputStream = newProcess.StandardOutput;
                        output = outputStream.ReadToEnd();
                    }
                    else
                    {
                        output = "Timed out at " + timeoutSeconds + " seconds waiting for " + exeName + " to exit.";
                    }
                }
            }
            catch (Exception e)
            {
                throw (new Exception("An error occurred running " + exeName + ".", e));
            }
            finally
            {
                outputStream.Close();
            }

            return "\t" + output;           

        }
    }
}

This is the way I am passing arguments from my main program (Form1.cs)

private void button1_Click(object sender, EventArgs e)
        {
            string output;
            output = LaunchEXE.Run(@"C:\Program Files (x86)\MyFolder\MyConsole.exe", "/BACKUP C:\\MyBackupProfile.txt", 100);
            System.Windows.Forms.MessageBox.Show(output);
        }

The command line tool accepts the following command and works perfectly:

C:\Program Files (x86)\MyFolder>MyConsole.exe /BACKUP C:\MyBackupProfile.txt

like image 779
Vivian Lobo Avatar asked Mar 12 '13 12:03

Vivian Lobo


1 Answers

I have two options for you -

1) Please try running your Visual Studio on "administrator mode". or 2) Try to implement this instead. https://github.com/commandlineparser/commandline.

"The Command Line Parser Library offers to CLR applications a clean and concise API for manipulating command line arguments and related tasks. It allows you to display an help screen with an high degree of customization and a simple way to report syntax errors to the user. Everything that is boring and repetitive to be programmed stands up on library shoulders, letting you concentrate yourself on core logic. This library provides hassle free command line parsing with a constantly updated API since 2005."

Worked great for me.

like image 133
PM_ME_YOUR_CODE Avatar answered Sep 20 '22 16:09

PM_ME_YOUR_CODE