views:

224

answers:

5

Exact duplicate:

Split string containing command-line parameters into string[] in C#


C#: How do you configure for a string[] to accept input of a whole string the way the console's main entry point accepts it broken up?

When you execute a console application made in c#, it's main method accepts any other arguments typed into it while executing into it's string[] args parameter broken up into an array in args[].

How can you accomplish the same thing if you're not using a console application?

A: 

String.Split()?

Arnshea
No, it is more complicated than that, because your input string might contain quoted spaces. Better write a parser which reads token by token.
0xA3
+2  A: 

I've had good luck with Mono.GetOptions. I had to copy it out of the Mono project and add it as a project within my solution, but it works great.

Chris Doggett
+1  A: 

I'll take a different tack on this one... Do you mean you want to get arguments in your Windows Forms application like in a Console app?

Just replace your Main() with

static void Main ( string[] args )
Moose
why the downvote? I thought I ansswered his second paragraph fairly well. the standard for a WinForms is static void Main()...
Moose
+4  A: 

The Windows command API handles breaking up everything following the executable name into an array, and passes that to the executable. So from the .NET standpoint, the runtime simply accepts an already-split string from the caller.

To mimic the way the argument string is split, you need to split on spaces outside of quotes. There's a few ways to accomplish this. There's a very good way already covered here.

Rex M
A: 

You mean like params? A broken up string of a varying number of method parameters?

Hawker