tags:

views:

299

answers:

2

I have referenced this thread and the website it points to: http://stackoverflow.com/questions/9161/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(1))
    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.

+1  A: 

In the properties for your application select the Debug side-tab, add some command-line arguments in the supplied input box and run the program from within the IDE. This way you should get exactly what error is occurring to cause your application to crash.

RobS
A: 

It would be hard to real-time debug a program meant to be invoked via the Open With context menu option, since it's launched directly by the OS.

It might help to know the text of the error you're getting. It could be that when Windows invokes a .NET app via Open With, it doesn't populate My.Application.CommandLineArgs at all. Or it might put the file name further down in the collection. You could try

For Each s As String In My.Application.CommandLineArgs
    MessageBox.Show(s)
Next

to see exactly how many items are there and their values. In any case, I would do some Google searching on how to intercept the context argument sent to an application using Open With.

Chris Tybur
AFAIK the "open with..." command should just put the filename as the first command-line argument. Setting the command-line arguments via the IDE debug options should enable working out what is causing the application to crash. My guess is that the application is trying to open the file supplied by the command-line arguments before it's done other necessary work, but without the application error we'll never know.... over to you MaQleod.
RobS