我无法像Environment.GetCommandLineArgs()
事件一样让StartupNextInstance
在Load
事件中工作。下面的代码检查应用程序是否获得了命令行参数,并将文件路径发送到FileOpen()
函数,该函数基本上通过将文件名放入其参数中来打开程序中的文件。
If Environment.GetCommandLineArgs().Length > 1 Then FileOpen(Environment.GetCommandLineArgs(1))
上面的代码在Load
事件中完美运行,但它在StartupNextInstance
事件中不起作用。我还尝试了下面的代码来获取命令行args的文件路径:
Private Sub MyApplication_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
Dim strFile As String = Environment.CommandLine
If strFile.Length > 0 Then frmMain.FileOpen(strFile)
End Sub
上面代码的问题在于它没有获取文件路径,而是获取用于打开程序的文件(使用Open with ...方法,当您右键单击文件时),它会打开程序的exe文件的位置。
当我尝试e.CommandLine时,我收到一条错误消息:
类型'System.Collections.ObjectModel.ReadOnlyCollection(Of String)'的值无法转换为'String'。
答案 0 :(得分:3)
您可以处理应用程序的StartupNextInstance event并使用e.CommandLine
参数检索所有新传递的参数的列表。
Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
If e.CommandLine.Count > 0 Then frmMain.FileOpen(e.CommandLine(0))
End Sub
除Environment.GetCommandLineArgs()
外,e.CommandLine
不包含应用程序的可执行路径作为第一项。