OnTheAnvil Posted April 13, 2005 Posted April 13, 2005 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 Quote
PWNettle Posted April 13, 2005 Posted April 13, 2005 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) /// <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: 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. Quote
Leaders snarfblam Posted April 14, 2005 Leaders Posted April 14, 2005 The System.Environment.GetCommandLineArgs() function returns an array of strings that contain the command line arguments passed to the application. 'VB Example: If System.Environment.GetCommandLineArgs()(0).ToUpper() = "-AUTORUN" Then 'Do what you need to do Else 'Prompt the user for options Endif Quote [sIGPIC]e[/sIGPIC]
OnTheAnvil Posted April 14, 2005 Author Posted April 14, 2005 Thanks for the fast response. I'll have to give them both a try. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.