I want to pass a string to main, but I am having trouble with spaces.
I pass the string "C:\Documents and Settings\desktop\..." to the Main(string[] args) and I had:
args[0] = "C:\Documents"
args[1] = "and"
args[2] = "Settings\desktop\..."
But what I want is:
args[0] = "C:\Documents and Settings\desktop\..."
Any way to keep spaces but concatenate to one element of the string? The code should also work with any number of spaces in a given file path, not just 2 in this case.
This is typically handled by passing the arguments in quotes.
For example, if you call this as:
You’ll get the string in the first arg (
args[0]).For example, using this program:
If you run this with the command line argument as:
(With the quotation marks in place), this will print:
If that isn’t an option, and you only have a single argument, you could always join the results:
This will work without wrapping the path in quotes, but it assumes the entire path is a single path, only has one space in between “words”, and has no other arguments in place. Personally, I would recommend wrapping the call in quotation marks, as it’s a standard convention.