Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
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:
Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads : 647
Total page views :  178181
Rating :
 4.33/5
This article has been rated :  3 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
CmdLineArgsCode.zip
 
Become a Sponsor


Related EbooksTop Videos

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
 [Top] Rate this article
 About the author
 
Mahesh Chand
Mahesh is a software developer with over 13 years of 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 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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
CmdLineArgsCode.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Powerful ASP.NET Hosting w/ NO Setup Fees. Click Here!
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 | Delete | Modify | 
Re: C# for beginners by Vipin On October 1, 2007
Balaguruswamy's C#.Net Fundamentals
Reply | Email | Delete | 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 | Delete | Modify | 
Rate this article by Deepakshi On October 14, 2008
4.5/5
Reply | Email | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved