Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get the arguments to my application in the original form (e.g including quotes)?

I have a .Net application that take a bunch of command line arguments, process some of it, and use the rest as arguments for another application

E.g.

MyApp.exe foo1 App2.exe arg1 arg2 ...

MyApp.exe is my application, foo1 is a parameter that my application care. App2.exe is another application, and my application will run App2 with arg1 arg2, etc. as arguments.

Currently my application just run App2.exe using something like this

Process.Start(args[1], String.Join(" ", args.Skip(2)). So the command above will correctly run: App2.exe with arguments "arg1 arg2". However, consider something like this

MyApp.exe foo1 notepad.exe "C:\Program Files\readme.txt"

The code above will not be aware of the quotes, and will run notepad.exe with arguments C:\Program Files\readme.txt (without quotes). How can I fix this problem?

like image 651
Louis Rhys Avatar asked Dec 03 '22 23:12

Louis Rhys


1 Answers

Environment.CommandLine

will give you the exact command line - you'll have to parse out the path to your app but otherwise works like a charm - @idle_mind alluded to this earlier (kind of)

Edited to move example into answer (because people are obviously still looking for this answer). Note that when debuging vshost messes up the command line a little.

#if DEBUG             
    int bodgeLen = "\"vshost.\"".Length; 
#else             
    int bodgeLen = "\"\"".Length; 
#endif              
string a = Environment.CommandLine.Substring(Assembly.GetExecutingAssembly().Location.Lengt‌​h+bodgeLen).Trim();
like image 173
mp3ferret Avatar answered May 10 '23 17:05

mp3ferret