Results 1 to 4 of 4

Thread: CMD Arguments

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Posts
    41

    CMD Arguments

    Hi Guys,

    Im trying to write a console application with arguments.

    i have done simple ones ie: file.exe "input" "output"

    however i want to do this with specific arguments ie:

    file.exe -i "something" -sub "Test" - To "nick@home.com" -Cc "someone" -body "body text" etc.

    Can anybody help me with this please.

    Cheers

    Nick

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: CMD Arguments

    You get the commandline arguments using Environment.GetCommandlineArgs, which returns a String array. You can use Array.IndexOf to determine which of your flags are present and where they are. For each flag that requires a value, you get the element at the next index and that's your value.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Posts
    41

    Re: CMD Arguments

    Quote Originally Posted by jmcilhinney View Post
    You get the commandline arguments using Environment.GetCommandlineArgs, which returns a String array. You can use Array.IndexOf to determine which of your flags are present and where they are. For each flag that requires a value, you get the element at the next index and that's your value.
    Cheers Pal.

    I will try this shortly :-)

    Nice one.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    18

    Re: CMD Arguments

    You can use the Main method found at Program.cs class,adding the string[] args as parameter and use it.
    And you can write a simple code that receive all arguments and they values.

    I wrote a simple algorithm that store all command args and it values in a Dictionary:

    Code:
    Dictionary<string, string> commandArgs = new Dictionary<string, string>();
    
    for (int i = 0; i < args.Length; i += 2)
    {
     if (args.Length < i)
      break;
    
     if (args[i].StartsWith("-") && args[i].Length > 1)
     {
      if (args.Length > (i + 1) && !args[i + 1].StartsWith("-"))
      {
       commandArgs.Add(args[i], args[i + 1]);
      }
      else
      {
       --i;
       continue;
      }
     }
     else
     {
      --i;
      continue;
     }
    }
    You can use like:

    Code:
    string body = commandArgs["body"];
    C#/VB.NET Themes/Open Source Projects: http://dotnettotal.comuf.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width