0

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?

Community
  • 1
  • 1

5 Answers5

4

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.

Community
  • 1
  • 1
Rex M
  • 142,167
  • 33
  • 283
  • 313
2

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
  • 19,959
  • 4
  • 61
  • 86
1

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
  • 5,354
  • 3
  • 33
  • 46
  • why the downvote? I thought I ansswered his second paragraph fairly well. the standard for a WinForms is static void Main()... – Moose Apr 14 '09 at 22:04
0

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

ahawker
  • 3,306
  • 24
  • 23
-1

String.Split()?

Shea
  • 11,085
  • 2
  • 19
  • 21
  • No, it is more complicated than that, because your input string might contain quoted spaces. Better write a parser which reads token by token. – Dirk Vollmar Apr 14 '09 at 21:56