【问题标题】:How to open multiple files using "open with" VB.net? (command line arguments)如何使用“打开方式”VB.net 打开多个文件? (命令行参数)
【发布时间】: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


【解决方案1】:

据我所知,当您在资源管理器中选择多个文件时,Windows 不会在同一命令行上发送多个文件名。对于某些应用程序,它将作为程序的单独实例启动,并将每个实例传递一个文件。

【讨论】:

  • 这是有道理的...我想知道是什么决定了它是只打开第一个(就像我一样)还是尝试启动多个实例
猜你喜欢
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 2012-03-15
相关资源
最近更新 更多