.net – My.Application.CommandLineArgs – can’t open file

vb.net

I have referenced this thread and the website it points to:
Opening a file in my application from File Explorer

As of now, I can't get My.Application.CommandLineArgs to work properly. Any time I select an file to open in my program I get a windows error forcing the program to close. I only want to get the file path from My.Application.CommandLineArgs and pass it to a function that will then parse the file (a text file of various extensions, even if it is not associated with my program) and display the contents in a RichTextBox.
My function to open the file is complete and works, but if I try to use My.Application.CommandLineArgs on a file the program crashes immediately.
here is the code I'm trying as a test:

Private Sub ParseCommandLineArgs()
    If My.Application.CommandLineArgs.Count > 0 Then
        MessageBox.Show(My.Application.CommandLineArgs(0))
    Else
        MessageBox.Show("No args")
    End If
End Sub

If I just open the application, I get the message box telling me no args were supplied, if I try to use "open with" my application for any file my application crashes. I have this function called during form_load.

Best Answer

I am afraid it's a classic off-by-one error, VB.NET Arrays and Collections start at element 0, so use:

 MessageBox.Show(My.Application.CommandLineArgs(0))
Related Topic