2012-05-15 58 views
1

我有一个应用程序可以读取某些类型的文件,并且让它工作,所以如果您从窗口“打开”,它会自动启动应用程序并打开选定的文件。如何使用“打开”VB.net打开多个文件? (命令行参数)

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

System.Environment.GetCommandLineArgs()contrains以下: 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指定它们,例如,设定的目标替代:

"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

+0

我不知道Windows如何处理与多个文件打开时,它可能多次使用给它开始每个实例一个文件,运行程序。 – Kratz

回答

1

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

+0

这是有道理的...我不知道是什么决定它是否只是打开第一个(像它一直为我)或如果它试图启动多个实例 – Allen

相关问题