Medic3000
Medic3000

Reputation: 786

how do I create a command line argument

I currently have an .exe I am running in my C# code, the .exe collects a list of files from specific locations, once it runs it prompts user to press esc to exit or enter to re-collect. Because I am trying to run this as an automated process from my code I would like to add a command line argument like -s or something to bypass the prompt for user input. without bypassing this the .exe will never close and the software will eventually crash. Here is my .exe code

class StackGrabber
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        try
        {
            StackReader.MessageHandler.OnMessage += new StackReader.MessageHandler.NotificationHandler(OnNotification);

            StackGrabber grabber = new StackGrabber();
            grabber.Grab();
        }
        catch (Exception ex)
        {
            DisplayMessage(ex.ToString());
        }
    }

    private volatile bool IsComplete = false;
    private StackReader.StackReader reader;

    void Grab()
    {
        try
        {
            DisplayMessage("Grabbing stacks... \n");

            reader = new StackReader.StackReader();
            reader.OnReadComplete += new System.Windows.Forms.MethodInvoker(OnComplete);

            if (reader.ReadStacks() > 0)
            {
                while (!reader.IsComplete)
                    Thread.Sleep(500);
            }
            else
            {
                Thread.Sleep(500);
            }
            DisplayMessage(Environment.NewLine + "Press esc to exit" + Environment.NewLine);
               //...
               //...
            Console.Read();
        }
        catch (Exception ex)
        {
            DisplayMessage(ex.ToString());
        }
    }

    
    public static void OnNotification(string msg)
    {
        DisplayMessage(msg);
    }

    public void OnComplete()
    {
        IsComplete = true;
    }

    private static object ConsoleLock = new object();
    private static void DisplayMessage(string msg)
    {
        lock (ConsoleLock)
        {
            Console.WriteLine(msg);
        }
    }
}

and I call the executable like this:

StackGrab.StartInfo.FileName = @"..\\bin\\StackGrabber.exe";
            StackGrab.StartInfo.CreateNoWindow = true;
            StackGrab.StartInfo.UseShellExecute = false;
            StackGrab.Start();
            StackGrab.WaitForExit();

I want to be able to call StackGrabber.exe in a command prompt so that I put C:\>StackGrabber -s and it doesnt wait for 'esc' input to exit

NOTE: this is a question about my software using C#2.0, Visual Studio 2005, and WinXP 32bit

Upvotes: 0

Views: 1081

Answers (3)

Fr33dan
Fr33dan

Reputation: 4327

You could add a boolean flag to your StackGrabber class to indicate if the user should not be prompted. You'll need to set it to true at initialization. Then set use Array.Exists on the args to find your parameter.

if(Array.Exists(args, delegate(string s) { return s.Equals("-s"); }))
    grab.PromptUser = false;

Then check it later in the grab function:

if(this.PromptUser)
    Console.Read();

Edit:The args parameter of of the Main function is the command line parameters that were supplied to the program. Here is an article on how to use command line arguments.

Upvotes: 1

in Static void Main(string[] args), args is your list of command line arguements.

to add command line arguements from the debugger you can right click on your project and go to "Properties"
Click on the debug tab
under the Start Options header there will be a box called "Command Line Arguements".

You can enter your arguements there

Upvotes: 0

gb92
gb92

Reputation: 116

The StartInfo has an Arguments or CommandArguments member of type string that you can use to pass in arguments

Upvotes: 1

Related Questions