Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access command line parameters outside of Main in C#

I am writing a .NET class that needs to parse the command line of the process. I don't want to have a dependency between the Main() method and that class. How can the class access the command line?

like image 291
Antoine Aubry Avatar asked Apr 12 '09 23:04

Antoine Aubry


3 Answers

Call Environment.GetCommandLineArgs().

like image 140
itowlson Avatar answered Sep 30 '22 19:09

itowlson


If you use .NET Compact Framework, Environment.GetCommandLineArgs() method isn't implemented and System.Diagnostics.Process.GetCurrentProcess().StartInfo.Arguments returns always empty string, so you must use main function and pass arguments to your others classes.

An example :

[MTAThread] static void Main(String[] commandLineArguments) {   CommandLineHelper.parse(commandLineArguments); }  public static class CommandLineHelper {   public static void parse(String[] commandLineArguments) {     // add your code here   } } 
like image 30
Stéphane B. Avatar answered Sep 30 '22 19:09

Stéphane B.


System.Diagnostics.Process.GetCurrentProcess().StartInfo.Arguments
like image 38
Scott Langham Avatar answered Sep 30 '22 21:09

Scott Langham