【发布时间】:2012-05-15 16:49:27
【问题描述】:
我有一个可以读取某些类型文件的应用程序,并且我让它工作,因此如果你从 Windows 中“打开”,它会自动启动应用程序并打开选定的文件。
很遗憾,我无法让它为多个文件工作。
System.Environment.GetCommandLineArgs() 包含以下内容: System.Environment.GetCommandLineArgs(0) = .exe 的名称和路径 System.Environment.GetCommandLineArgs(1) = 选择要打开的第一个文件的名称和路径
当用户尝试打开 1 个文件时,System.Environment.GetCommandLineArgs().Length 为 2,这是有道理的,因为第一个参数是 .exe 本身,第二个是文件的路径,但它不会增加如果用户尝试打开 2 个文件,则为 3...这意味着 System.Environment.GetCommandLineArgs(2) 永远不会被填充
这里是一些显示问题的示例代码:它会识别没有文件或打开 1 个文件,但如果您尝试打开多个文件,它只会显示第一个。
Private Sub Form_Main_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Show()
' Check if the user is opening a file upon startup
If System.Environment.GetCommandLineArgs().Length > 1 Then
Dim i As Integer
'this outputs the exe path and the the first file, if it exists, but never the 2nd file...
'For i = 0 To System.Environment.GetCommandLineArgs().Length - 1
' MsgBox(System.Environment.GetCommandLineArgs(i))
'Next
'this outputs the first file, if it exists, but never the 2nd file...
For i = 1 To System.Environment.GetCommandLineArgs().Length - 1
MsgBox(System.Environment.GetCommandLineArgs(i))
Next
End If
End Sub
我有什么遗漏吗?有没有使用 System.Environment.GetCommandLineArgs() 的替代方法
另外,我注意到如果我在 .exe 的快捷方式中指定它们,我确实可以有多个命令参数,例如,设置目标:
"C:\Program Files\Reader\Reader.exe" -today -tommorow
当我以这种方式运行它时,我得到:
System.Environment.GetCommandLineArgs().Length = 3
System.Environment.GetCommandLineArgs(0) = "C:\Program Files\Reader\Reader.exe"
System.Environment.GetCommandLineArgs(1) = "-today"
System.Environment.GetCommandLineArgs(2) = "-tomorrow"
这是我所期望的......
如果有帮助,我正在使用 Windows XP
【问题讨论】:
-
我不确定 Windows 如何处理打开多个文件的问题,它可能会多次运行您的程序,并为它启动的每个实例提供一个文件。
标签: vb.net file-io command-line arguments open-with