-
Sep 17th, 2014, 03:40 AM
#1
Thread Starter
Member
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
-
Sep 17th, 2014, 04:13 AM
#2
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.
-
Sep 17th, 2014, 04:56 AM
#3
Thread Starter
Member
Re: CMD Arguments
Originally Posted by jmcilhinney
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.
-
Sep 17th, 2014, 10:37 AM
#4
Junior Member
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"];
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|