Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

how to read a console application c#

Back to Forum: Tech Off
  • User profile image
    blackCoffee

    Hi, can someone please help me, how could  I convert a string input in a console like:

    -u username -p password 

    to get username and password as parameters for my procedure. Can i read the input and make an '' if i fine -u put the rest of the string in the parameter username''? is this making any sens? Can someone please help, i assume that it's a very simple thing to do but I'm a begginer and I'm tring to learn Perplexed please help me!

  • User profile image
    wkempf

    There are much better forums for programming help. That said, the command line arguments can be found in the args parameter passed to your Main function. You can parse that yourself, or you can search Google for a command line parsing library.

  • User profile image
    JohnAskew

    @wkempf: Bing!

    Big Smile

  • 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

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.