1

Is it possible to pass whitespace to a C# program as a command line argument?

I am writing a script that parses a text document, and would like to allow users to specify their own separator, rather than hard-coding one.

However, if the separator is a space, then the program appears to discount it as an argument, whether surrounded by quote marks or not.

Ollyver
  • 359
  • 2
  • 14
  • See http://stackoverflow.com/questions/6427732/how-can-i-escape-an-arbitrary-string-for-use-as-a-command-line-argument-in-windo – Tarik Oct 15 '13 at 16:37
  • if on command line you do `yourProgram.exe "test" " "` then it will receive two arguments, one `test` the other one `" "` which is space, This should work. – Habib Oct 15 '13 at 16:42

3 Answers3

1

Environment.CommandLine will give you the complete command line, including any whitespace. Although I think it would be tricky for your users to pass tabs or line breaks. I'd suggest having them pass the char code(s) for the separator(s) instead.

Chris
  • 5,442
  • 17
  • 30
1

This might help:

Escape command line arguments in c#

From what some other similar questions say, using Regex seems to be the way to go.

(Not really done much C# I'm afraid, but just saw this on the homepage)

Community
  • 1
  • 1
Dangercrow
  • 145
  • 1
  • 11
1

you can pass in quoted whitespace " " For example

FileParsing.exe arg1 " " arg3 "some other arg"

Chad Dienhart
  • 5,024
  • 3
  • 23
  • 30
  • This gives me an IndexOutOfRangeException - i.e. it thinks it's only had 3 arguments, not 4. (I explicitly count the arguments, to give the user feedback if their input is missing something, and it definitely has issues with " ".) – Ollyver Oct 16 '13 at 09:09
  • It worked here for me. I'm on Windows 7 and running the standard cmd.exe – Chad Dienhart Oct 17 '13 at 12:59