How To : add command line parameters to an windows application

  • Thread starter Thread starter Bill Devoac
  • Start date Start date
Bill,

This is easy. The first parameter of your entry point (typically the
Main method), will take an array of strings. This array represents the
command line that was passed into the application. From there you can
interpret it any way you wish.

Hope this helps.
 
Simply change the signature of Main to take a string[]:

static void Main(string[] argv)
{
}
 
Bill said:
How do we add command parameters to a windows application?

I would suggest to use a very good command-line parser from Peter Hallam

See: http://www.gotdotnet.com/Community/UserSamples/Details.aspx?
SampleGuid=62a0f27e-274e-4228-ba7f-bc0118ecc41e

Can be downloaded from
http://www.gotdotnet.com/Community/UserSamples/Download.aspx?SampleGuid=
62A0F27E-274E-4228-BA7F-BC0118ECC41E


You only need to specify a class like:

internal class Args
{
public bool auto = false;
public string logDir = null;
public string testFile = null;
}

And everything is finished!!! (incl. usage-Description)
You can also specify some Attributes...

<code>
static int Main(string[] args)
{
Args myArgs = new Args();
if (!Utilities.Utility.ParseCommandLineArguments(args, myArgs))
{
Console.WriteLine((Utilities.Utility.CommandLineArgumentsUsage(
typeof(Args)));
return 1;
}
// here you can use the (filled) class

}
</code>

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp


Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
Hi Bill

public static void Main(string[] args)
{
}

args[0] will be the startup path (I think) and args[1] and so on the
parameters.

Happy coding!
Morten
 
Bill Devoac said:
Hi,
How do we add command parameters to a windows application?
Thanx

In addition to the other suggestions, you can generally get to the command
line parameters anywhere by using
System.Environment.GetCommandLineArgs();
 
Back
Top