ARTICLE

Access Command Line Arguments in C#

Posted by Mahesh Chand Articles | How do I March 27, 2006
This article shows how to access command line arguments in C#.
Reader Level:
Download Files:
 

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();

}

I compile the above code and run the exe from the command line by passing following parameters:

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);

}

Download and run the attached code for more details.

Login to add your contents and source code to this article
Article Extensions
Contents added by eak naja on Jan 07, 2013
thank you
post comment
     

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?

Posted by elham deljooei Apr 29, 2013

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

Posted by Dinesh Mar 14, 2011

hello, how I can put the args in a label for the user see?


ty

Posted by Mateus Teruel Jul 24, 2010

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

Posted by Ian Oldbury Jul 21, 2010

How can we passing parametres using office hyperlink?

Posted by Batikan Duru Jul 16, 2010
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.