【问题标题】:Passing parameters and filename?传递参数和文件名?
【发布时间】:2011-10-21 20:29:34
【问题描述】:

我需要编写一个 VB.Net Winform 应用程序,该应用程序通过 Windows 资源管理器中的上下文菜单调用,并且可以采用多个参数,其中最后一个参数是包含空格的文件的完整路径。

我尝试了以下作为参数,但无济于事:

This is a test "%1" -> this
"This is a test "%1"" -> This is a test C:\Program
"This is a test" "%1" -> This is a test
"This is a test "%1%"" -> This is a test

有人知道如何让 Windows 将完整路径传递给文件名吗?

谢谢。

【问题讨论】:

  • 你的意思是hkcr\filetype\shell\operation的值吗?
  • 不,最后一个参数是文件名,其中可能包含空格。我无法让源应用程序将整个字符串传递给我的 VB.Net 应用程序:-/

标签: windows vb.net


【解决方案1】:

Windows 正在传递完整路径。你只是在解析它时遇到了问题。

This is a test "%1" -> This is a test "C:\Program Files\Blah blah"
-> cmdArgs(0) = "This"
-> cmdArgs(1) = "is"
-> cmdArgs(2) = "a"
-> cmdArgs(3) = "test"
-> cmdArgs(4) = "C:\Program Files\Blah blah"

在上面的示例中,由于短语“This is a test”中的单词没有引号,因此它们作为单独的命令行参数传递。

"This is a test "%1"" -> "This is a test "C:\Program Files\Blah blah""
-> cmdArgs(0) = "This is a test "
-> cmdArgs(1) = "C:\Program"
-> cmdArgs(2) = "Files\Blah"
-> cmdArgs(3) = "blah"

在第二个示例中,您在 %1 之前插入的引号终止了第一个参数,而实际文件名未加引号。

"This is a test" "%1" -> "This is a test" "C:\Program Files\Blah blah"
-> cmdArgs(0) = "This is a test"
-> cmdArgs(1) = "C:\Program Files\Blah blah"

在第三个示例中,两个参数分别被引用,因此它们作为两个参数干净地到达您的程序。这是您应该使用的。您的问题是您只查看cmdArgs(0) 而忽略了cmdArgs 其余部分中的其他内容。特别是,您忘记检查文件名所在的cmdArgs(1)

【讨论】:

  • 非常感谢雷蒙德。我认为所有参数都作为一个字符串发送。循环通过 My.Application.CommandLineArgs 让我检索所有这些。
猜你喜欢
  • 1970-01-01
  • 2019-03-18
  • 2011-07-06
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多