2012-05-15 57 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

相關問題