在命令行参数中获取文件

Get a file in command line args

我想在命令行参数中获取一个文件

例如:myapp.exe /File "C:\Path\To\File.txt"

我编写了一个无效的代码:

myfile = CommandLineArgs(2).Remove(0, CommandLineArgs(2).IndexOf(Chr(34)))

我遇到了这个错误:

In English : The account cannot be less than zero. Setting name: count

有人可以帮助我吗?

如果你总是有一对参数,那么你可以使用这样的代码:

Private myfile As String

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim args() As String = Environment.GetCommandLineArgs
    ' The executable itself is at args(0), so we start a 1 below:
    For i As Integer = 1 To (args.Length - 2) Step 2
        Select Case args(i).ToUpper
            Case "/FILE"
                myfile = args(i + 1)
                Debug.Print("myfile = " & myfile)

            Case "/OTHEROPTION"
                ' ... code ...

            Case Else
                Debug.Print("Unknown argument: " & args(i))
                Debug.Print("Unknown value: " & args(i + 1))
        End Select
    Next
End Sub