My question is rather unique and I couldn’t find any solution yet.
I had a WinForms program that processed the Command line arguments (it is integrated into the context menu and it is called with the directory name of which it was opened on). It worked like charm. The call was like this: myprogram.exe '%1' – where %1 is the directory name.
I tried to create a WPF application with the same method: integrated into the context menu, with the same signature. (myWPFprogram.exe '%1'), using Environment.GetCommandlineArguments().
There is a directory existence check at the beginning of the program. In the WinForms program it has never been triggered (as it was run from the directory I clicked on, so it should have been strange not being existing). With my WPF program I ALWAYS get the message:
An error occurred: The provided directory (‘d:\workbench\dialogs’) doesn’t exist.
I clicked on that directory so it is existing.
Other strange thing that if there is a space in the directory’s name, it becomes ‘truncated’ like in old DOS-times:
An error occurred: The provided directory (‘d:\workbe~1\testsp~1’) doesn’t exist.
How should I get the proper directory name from WPF Command line? What is the problem with my approach?
The problem is that you enclose the argument in single quotes. This doesn’t work, and the argument your program receives is actually something like
'd:\workbench\dialogs'(notice the single quotes).If you want to fix this, use double quotes. They are recognized and the argument you are going to receive will be something like:
d:\workbench\dialogs. I have no idea why this worked with your WinForms application, because it should behave exactly the same.The fact that long names are converted to short names is there probably for compatibility purposes and it shouldn’t bother you. Both versions of the path should work equally fine. If you want to get the normal version of the path, you can use
Path.GetFullPath().