Get a program's command line arguments in C#
One method to do this is to override the Main method and give it a parameter that is a string array.
static void Main(string[] args)I prefer the following method, partly because you don't need to mess with Main and partly because it lets you check the command line arguments anywhere in the program.
{
foreach (string arg in args)
{
lstArguments.Items.Add(arg);
}
}
Form1_Load(object sender, EventArgs e)The first argument is always the fully-qualified name of the executing program. This technique is interesting but even more interesting are the ways you can send arguments to the program. You can:
{
foreach (string arg in Environment.GetCommandLineArgs())
{
lstArguments.Items.Add(arg);
}
}
- Set them in the IDE. Open the Project menu, select Properties (at the bottom), click the Debug tab, and enter the arguments in the "Command line arguments" TextBox. (This is mostly useful for testing.)
- Execute the program at a command prompt (for example, the Run command or cmd.exe) and follow it with parameters
- Drag and drop one or more files or folders onto the executable program.
- Right-click and send a file or folder to the executable program added in the Send To menu (see Add items to the "Send To" menu).


Hi!The download url for this entry is incorrect,could you fix it?
Reply to this
Sorry about that. I've fixed it. Thanks for pointing this out!
Reply to this