如何使用“open with”VB.net打开多个文件? (命令行参数)

时间:2012-05-15 16:49:27

标签: vb.net file-io command-line arguments open-with

我有一个应用程序,可以读取某些类型的文件,我有它的工作,如果你从Windows“打开”,它会自动启动应用程序并打开所选文件。

不幸的是,我不能让它为多个文件工作。

System.Environment.GetCommandLineArgs()禁止以下内容: System.Environment.GetCommandLineArgs(0)= .exe的名称和路径 System.Environment.GetCommandLineArgs(1)=选择打开的第一个文件的名称和路径

System.Environment.GetCommandLineArgs()。当用户尝试打开1个文件时,长度为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的快捷方式中指定它们,我确实可以有多个命令参数,例如,设置Target:

"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

1 个答案:

答案 0 :(得分:1)

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