Wrap for command line tool in C #

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

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

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

I wanted to know if I am passing the arguments correctly, and if I am mistaken, could someone please indicate. 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 how I pass 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 fine:

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

+5
2

-

1) Visual Studio " ". 2) . http://commandline.codeplex.com/.

" Parser CLR API . . , , , , . API 2005 .

.

+1
0
source

All Articles