I'm reading a shortcut using the COM "Windows Script Host Object Model" IWshRuntimeLibrary like this:
IWshShell wsh = new WshShellClass();
IWshShortcut sc = (IWshShortcut)wsh.CreateShortcut(shortcutFile);
string arguments = sc.Arguments;
This gives me the arguments from the shortcut, say it looks like:
-a "c:\temp\a 1.txt" -b "b.exe -c \"c:\temp\c -2.txt\" -d d.txt" -e test
Which is not as a contrived example as you may think... How can I do the same as GetCommandLineArgs and get:
args {string[6]}
[0] = "-a"
[1] = "c:\\temp\\a 1.txt"
[2] = "-b"
[3] = "b.exe -c \"c:\\temp\\c -2.txt\" -d d.txt"
[4] = "-e"
[5] = "test"
I'm thinking Regex, but this is beyond my bracketing.
[edit] I'd prefer a regex, but I'll look at the code and classes mentioned.
The rules seem to be:
- Split on spaces,
- unless the space is within double quotes (").
- Strings within double quotes may contain double quotes escaped with a backslash (\").
- Other backslashes may appear on their own but should be treated as a normal character
The arguments and array in the previous two code sections would be an appropriate test.
[edit] https://stackoverflow.com/a/749653/350188 has answered my question. It calls the native system function and gives me exactly the same results as GetCommandLineArgs() with:
string[] splitArguments = CommandLineToArgs(arguments);