如果 arg 以“.lee”结尾,则阻止 arg 显示 MsgBox() - VB.Net

Prevent arg from showing MsgBox() if arg ends with ".lee" - VB.Net

我的程序 args 设置为 Dim args As String() = Environment.GetCommandLineArgs()。我在 args 中将 arg 用作 String。程序检查 arg 是否以 ".lee" 结束然后继续。否则,如果 arg".txt" 结尾,它仍会继续执行下一步。否则如果 arg 不符合要求,它应该弹出一个 MsgBox.

即使执行支持的类型文件,问题是什么,我的程序似乎显示错误对话框。

代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim args As String() = Environment.GetCommandLineArgs()
        For Each arg As String In args
            If arg.EndsWith(".lee") Then
                leeReader.RichTextBox1.LoadFile(arg, RichTextBoxStreamType.PlainText)
                TextBox1.Text = leeReader.RichTextBox1.Text
                Btn1.PerformClick()
            Else
                If arg.EndsWith(".txt") Then
                    leeReader.RichTextBox1.LoadFile(arg, RichTextBoxStreamType.PlainText)
                    TextBox1.Text = leeReader.RichTextBox1.Text
                    Btn1.PerformClick()
                Else
                    MsgBox("We don't recommend unsupported files")
                    Me.Close()
                End If
            End If
        Next
    End Sub

我尝试添加 Exit Sub 但它不起作用。

修改后的代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim args As String() = Environment.GetCommandLineArgs()
        For Each arg As String In args
            If arg.EndsWith(".lee") Then
                leeReader.RichTextBox1.LoadFile(arg, RichTextBoxStreamType.PlainText)
                TextBox1.Text = leeReader.RichTextBox1.Text
                Btn1.PerformClick()
                Exit Sub
            Else
                If arg.EndsWith(".txt") Then
                    leeReader.RichTextBox1.LoadFile(arg, RichTextBoxStreamType.PlainText)
                    TextBox1.Text = leeReader.RichTextBox1.Text
                    Btn1.PerformClick()
                    Exit Sub
                Else
                    MsgBox("We don't recommend unsupported files. Give it a try?")
                End If
            End If
        Next
    End Sub

如果您知道解决方案/如果我哪里出错/您需要更多帮助,请在下面回答/评论。

您正在遍历所有命令行参数,但是(来自文档)

The first element is the executable file name, and the following zero or more elements contain the remaining command-line arguments.

所以您总是会看到第一个参数的消息框。尝试:Environment.GetCommandLineArgs().Skip(1)