Recently, one user asked me how to access command line arguments in a C# application. This how do I answers the same.
There are two common ways to read command line arguments in C#. First, you can override the Main method with an array of strings, which are command line arguments. For example, the following code loops through the command line arguments and print them on the console.
- static void Main(string[] args)
- {
- foreach(string arg in args)
- {
- Console.WriteLine(arg);
- }
- Console.ReadLine();
- }

The output generated by the application looks like following:

However, this is not only the way to read command line arguments. For example, what if you do not want to override the Main method? Or access the command line arguments from non-Main method of your application?
You can do so using the Environment class, which has a static method called GetCommandLineArgs, which returns an array of strings containing the arguments. The following code reads the command line arguments using Environment.GetCommandLineArgs method.
- foreach (string arg in Environment.GetCommandLineArgs())
- {
- Console.WriteLine(arg);
- }

Suraj BinorkarPosted May 31, 2017, 5:32 AM
Thank you for sharing this :) actually I am facing some issue with the console application, in that i am trying to send three parameters to EXE <name><number><filename>, i have successfully done in C but not able to do in C#, So is there any way to do it by using command line arg in C#, please help me to solve this issue :)
elham deljooeiPosted Apr 29, 2013, 3:11 AM
Hi I can't understand what you mean? I've written a project but i don't know how i have to run it and pass parameter to it?
DineshPosted Mar 14, 2011, 6:35 AM
Hi, I want to pass ArrayList as command line argument to one another c# program. I am finding very difficult to do this. Can any one please help me in this regard? Thanks, Dinesh
Mateus TeruelPosted Jul 24, 2010, 12:14 AM
hello, how I can put the args in a label for the user see? ty
Ian OldburyeditedPosted Jul 21, 2010, 8:22 AMEdited Jul 21, 2010, 8:23 AM
hi, am creating a Console applcation and would like to be able to pass parameters into the application. in the format style of named key and value pairs... example....myConsoleApp.exe /param1 654321 /param2 654654654 having a look at your example would mean that i have 4 parameters. what is the best way of doing this, so that they are key/ value? Cheers Ian
Batikan DuruPosted Jul 16, 2010, 12:06 PM
How can we passing parametres using office hyperlink?
Sundar RajanPosted Apr 1, 2009, 8:11 AM
Environment.GetCommandLineArgs() returns the name of the program as the first element in the returned array where as Main(string[] args) does not. Also, I did not understand Mahesh's comment about overriding the Main method. The Main method is static and as such overriding should not have any meaning. Also which method are we overriding when we write a Main method? Thanks in advance
Jose MoralesPosted Mar 12, 2009, 3:28 PM
Please help me. I need to create a simple report program (C#) that read a SQL table and create a pdf file automatically (No user intervention). The program should not ask for any parameter or export like (excel, pdf), just run and create the pdf file.
Deepakshi kPosted Oct 14, 2008, 1:07 AM
4.5/5
eshiauPosted Feb 26, 2008, 4:01 PM
How to associate a file and pass file name to the app? For example, when user double click file.jpg and app will get file.jpg as command line and display file.jpg inside the app. Thanks.
Lawal MohammedPosted Jun 12, 2007, 4:18 PM
what is a good material for learning C# for beginners without programming experience