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?
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.Length+bodgeLen).Trim();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With