Command Line options with a windows app

OnTheAnvil

Regular
Joined
Nov 4, 2003
Messages
92
Location
Columbus, Ohio, USA
Is there any way to pass a windows application command line arguments or an equvalent? All I want to do is have a windows form that when you click the icon it opens and prompts the users for what they want to do. But I also want to run it at night so it can perform the standard set of options without use interaction, possible by doing this "C:\overnight.exe -Autorun". Is there anyway to accomplish this?

Thanks,
OnTheAnvil
 
I imagine this topic has been covered before...but here goes anyways...

In C# you can alter your winapp's entry point (Main() routine by default) to allow it to accept command line arguments like so:

(this code is autogenerated in your winform's code)

Code:
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main([b]string [] sArgs[/b]) 
{
	Application.Run([b]new Form1(sArgs)[/b]);
}

All you have to do is provide this simple way for your app to receive the command line arguments (usually done via a string array to allow for a potentially varying number of arguments). Windows will automatically pass in the command line values for you.

You could deal with your command line arguments in Main...but in the example above the args are passed on to the form's constructor, which has been similarly altered for accepting the args:

Code:
public Form1([b]string [] sArgs[/b])
{
	InitializeComponent();
	
	for (int i = 0; i < sArgs.Length; i++)
	{
		this.label1.Text += sArgs[i] + Environment.NewLine;
	}
}

The easiest way to setup an icon to fire your winapp with command line arguments is to make a shortcut to for your EXE and edit the shortcut properties to include the command line arguments. The "target" of your shortcut should be the full path to your EXE. Just stick your arguments after that (seperated by spaces). If you want to pass in an argument that contains spaces put it inside double quotes.

Paul

PS - If C# isn't your thing I'm sure this can be done almost exaclty the same in VB.Net.
 
The System.Environment.GetCommandLineArgs() function returns an array of strings that contain the command line arguments passed to the application.

Visual Basic:
'VB Example:

If System.Environment.GetCommandLineArgs()(0).ToUpper() = "-AUTORUN" Then
    'Do what you need to do
Else
    'Prompt the user for options
Endif
 
Back
Top