Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » How do I » Access Command Line Arguments in C#

Access Command Line Arguments in C#

This article shows how to access command line arguments in C#.

Author Rank :
Page Views : 327504
Downloads : 1328
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
CmdLineArgsCode.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
C# for beginners by Lawal On June 12, 2007
what is a good material for learning C# for beginners without programming experience
Reply | Email | Modify 
Re: C# for beginners by Vipin On October 1, 2007
Balaguruswamy's C#.Net Fundamentals
Reply | Email | Modify 
Associate file to command line argument by eshiau On February 26, 2008
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.
Reply | Email | Modify 
Rate this article by Deepakshi On October 14, 2008
4.5/5
Reply | Email | Modify 
Simple Silent Report Program C# by Jose On March 12, 2009
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.
Reply | Email | Modify 
Re: Simple Silent Report Program C# by Mahesh On April 2, 2009
You need to create a Windows Service that will talk to your database and generate reports. However, there is no direct API in .NET to generate PDF reports. You may need to buy a third party component such as DynamicPDF or ExpertPDF.

Here is a tutorial on how to create a Windows Service.
http://www.c-sharpcorner.com/Beginners/
Reply | Email | Modify 
Access Command Line Arguments in C# by Sundar On April 1, 2009
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
Reply | Email | Modify 
Re: Access Command Line Arguments in C# by Mahesh On April 2, 2009

Basically, you may have a class with say 3 different overloaded methods taking differnet types of arguments/parameters. Say if your application accepts one string param or int param or an array.

Reply | Email | Modify 
Passing Parametres by Batikan On July 16, 2010
How can we passing parametres using office hyperlink?
Reply | Email | Modify 
Parameter List by Ian On July 21, 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

Reply | Email | Modify 
put string arg in a label? by Mateus On July 24, 2010
hello, how I can put the args in a label for the user see?

ty
Reply | Email | Modify 
How to pass ArrayList as command argument to C# program? by Dinesh On March 14, 2011
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
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.