加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – StartupNextInstance事件中的Environment.GetCommandL

发布时间:2020-12-17 07:14:04 所属栏目:百科 来源:网络整理
导读:我在尝试让Environment.GetCommandLineArgs()在StartupNextInstance事件中以它在表单的Load事件中的方式工作时遇到了麻烦.下面的代码检查应用程序是否有命令行参数,并将文件路径发送到FileOpen()函数,该函数基本上通过将文件名放入其参数来打开程序中的文件.
我在尝试让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时,我得到一个错误说:

Value of type ‘System.Collections.ObjectModel.ReadOnlyCollection(Of String)’ cannot be converted to ‘String’.

解决方法

您可以处理应用程序的 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不包含应用程序的可执行路径作为第一项.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读