Getting raw (unsplit) command line in .NET

Try using: Environment.CommandLine


To get the unparsed, raw, unmodified command line you have to P/Invoke GetCommandLine from kernel32. Some parsing will take place by the operating system. For example, an IO redirection such as >foo.txt will be excluded from the command line text regardless of the technique used.

Environment.CommandLine may be sufficient, but be aware that it removes interstitial spaces between arguments (unless the argument itself is enclosed in quotes) and it removes the quotes from quoted arguments.

For example, for the command line:

test.exe this is "a test"

Environment.CommandLine equals: "this is a test"

But GetCommandLine yields: "test.exe this is "a test"" with the spaces and quotes intact, along with the path of the exe.

Note that when using this technique, you have to parse the command line text manually, which may involve stripping off the path to the exe, which may itself be enclosed in quotes if the path contains spaces.