简体   繁体   中英

Pass Argument in Command line and get full path

I have two Directory in D drive my PC

D:\My First Directory 
D:\My Second Directory 

when I am pass this two argument in console program in C#

like this :

Program.exe D:\My First Directory D:\My Second Directory 

I access this this of output.

args[0] : D:\My
args[1] : First 
args[2] : Directory 
args[3] : D:\My 
args[4] : Second 
args[5] : Directory 

I want to this type of output :

args[0] : D:\My First Directory 
args[1] : D:\My Second Directory 

The only way to do it is to pass the arguments inside double quotes, like this:

Program.exe "D:\My First Directory" "D:\My Second Directory"

There is no other simple way. Operating system just works like that. One string is one argument.

You could do some wise mechanism though. I wouldn't do it, I would just put those directories in double quotes.

But if I for some reason REALLY, REALLY had to do it, I'd do it like that:

  • get all the arguments into one string
  • magically split this string into list (array) of strings, using regular expressions (looking for string like: "D:\\", of course as a regular expression).

But it could only work if you can only have directories in arguments and nothing else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM