WPF 应用程序 CommandLineArguments 目录不存在
我的问题相当独特,我还没有找到任何解决方案。
我有一个处理命令行参数的 WinForms 程序(它集成到上下文菜单中,并使用打开它的目录名来调用它)。它就像魅力一样起作用。调用如下:myprogram.exe '%1'
- 其中%1
是目录名称。
我尝试使用相同的方法创建一个 WPF 应用程序:集成到上下文菜单中,并具有相同的签名。 (myWPFprogram.exe“%1”
),使用 Environment.GetCommandlineArguments()
。
程序开头有一个目录存在检查。在 WinForms 程序中它从未被触发(因为它是从我单击的目录运行的,所以它不存在应该很奇怪)。在我的 WPF 程序中,我总是收到以下消息:
发生错误:提供的目录('d:\workbench\dialogs')不存在。
我单击了该目录,因此它已存在。
另一件奇怪的事情是,如果目录名称中有空格,它就会像旧 DOS 时代一样被“截断”:
发生错误:提供的目录('d:\workbe~1\testsp~1')不存在。
我应该如何从 WPF 命令行获取正确的目录名称?我的方法有什么问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您将参数用单引号引起来。这是行不通的,你的程序收到的参数实际上类似于
'd:\workbench\dialogs'
(注意单引号)。如果您想解决此问题,请使用双引号。它们被识别,您将收到的参数将类似于:
d:\workbench\dialogs
。我不知道为什么这适用于您的 WinForms 应用程序,因为它的行为应该完全相同。事实上,长名称转换为短名称可能是出于兼容性目的,它不应该打扰您。路径的两个版本应该同样可以正常工作。如果您想获取正常版本的路径,可以使用
Path.GetFullPath()
。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()
.