Reading command-line arguments

  • Thread starter Thread starter Greg Smith
  • Start date Start date
G

Greg Smith

I want to read command-line arguments passed into my application. All the
examples I have found simply read the args in Main() and write then to the
console. I can't seem to make them visible other places in my application.

Any help is greatly appreciated.
 
Put the argument value in a global variable.

Thank you for your response.

I am still a bit new at this, could you give me an example?

Thanks again.
 
Personally I like to take the event args within my Main method and pass it
to the constructor of my WinForms application (assuming of course that your
app is a WinForms app) like this:

private static void Main(string[] args)
{
Application.Run(new frmMain(args));
}

And then in my constructor of my app I take the arguements and pass them off
to a method to be parsed out like so:

public frmMain(string[] args)
{
ParseCmdLine(args);
}

private void ParseCmdLine(string[] args)
{
// If args==null do nothing and return

// foreach through command line args and set private members
}

If you need a more detailed example or perhaps one for a console app - let
me know and I can post the code out on my site (http://dotnet.redeyepos.com)
rather than cluttering up the newsgroup.

- Brian Patterson
 
Hi,

You can access the commandline arguments through the
Environment.GetCommandLineArgs() method.

Hope this helps
 
Back
Top