Tech Off Post

Single Post Permalink

View Thread: how to read a console application c#
  • User profile image
    Sven Groot

    Not to beat my own drum, but I made a library for parsing command line arguments: http://ookiicommandline.codeplex.com/

    To use it for your example, you'd create a class as follows:

    class MyArguments
    {
        [CommandLineArgument("u")]
        public string UserName { get; set; }
        
        [CommandLineArgument("p")]
        public string Password { get; set; }
    }

    Then put the following code in your Main function:

    public static void Main(string[] args)
    {
        CommandLineParser parser = new CommandLineParser(typeof(MyArguments));
        try
        {
            MyArguments arguments = (MyArguments)parser.Parse(args);
            // Do stuff here
        }
        catch( CommandLineException ex )
        {
            Console.WriteLine(ex.Message);
            parser.WriteUsageToConsole();
        }
    }

    Big Smile