Assigning File Extensions and Using them

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
Sorry to ask for alot but I am self taught and this information is hard to find. I need to assign a certain file extension to my application. When the user clicks on this file type it will run in my application. Further; I need to know how to use this information in my application. How do I tell my application to use the file chosen to be started with it?

I apologize if this is too much but any help would be great. This includes books, links, or examples. Thankyou so much:)
 
Ok, I think I have the jist of that but I am a little lost on how i tell my application what to do with that information. For example: I double click on a text file; what Sub Procedure get's that command?
 
Environment.GetCommandLineArgs method. I'm a little short on time right now, here's what doc says:
returns "An array of string where each element contains a command line argument. The first element is the executable file name, and the following zero or more elements contain the remaining command line arguments."
 
Thanks, I am looking into that now. If you have an example it would be great. :) Thanks again, but hopefully I will figure it out.
 
VB.NET:
Dim cla As String() = Environment.GetCommandLineArgs()

If cla.Length > 1 Then
    If IO.File.Exists(cla(1)) Then
        MessageBox.Show("You have chosen to open the file " & cla(1))
    Else
        MessageBox.Show("Cannot find the file " & cla(1))
    End If
End If
 
Ok, that's great and pretty much what I am looking for but where do I put that in the code? B/C I have never used that I am assuming it goes into the startup module or form but can I put it into the my.application startup event?
 
Ok, I most be totally ignorant to as what is going on with this. Here is what I have done.

I placed in my installed to refer file the file types to my program. I then installed. Once installed the icons of that file type are the ones I chose and when I double click them my program starts. However; my program does not recognize me trying to start a particular file (the one I chose) it starts normally as it always does.

Now this makes since b/c there is no code in my program to look for anything of that nature. So I have placed the code you provided in my program right in the startup process. I didn't change it b/c I wanted to get the msgbox's just to test and debug. I never get a msgbox. This could be b/c the commandline never has more than 1 argument. Anyways, what I think is happening is that even though I am double clicking and sending the information to the program to start it, I am not sending the information to the program which file chose to start it. Or is this automatic?

Also, I did build and deploy the application b/c I am assuming you can't test this feauture in the IDE and have to test it as installed.

I know this must be a pain but if you or anyone can walk me through this I would be greatly appreciated. I feel like an idiot for not being able to figure this out and all b/c this is a popular feature among programs. However; with that said, I am ignorant as to what i'm doing with this procedure anyways.
 
Just like anything, you put this code where you want to use it. Where would you want the file to opened? Would it be when the main form is loaded or somewhere else? It is up to YOU to decide where is most appropriate for it to go based on how you want to use it. The most logical places would be in the Load event of your main form or in application's Startup event handler. To access the application events you go to the Application tab of the project properties and click the View Application Events button. Note that in the application Startup event handler you probably wouldn't use Environment.GetCommandLineArgs because you have available the e.CommandLine property. Note that e.CommandLine does NOT include the executable path as the first argument while Environment.GetCommandLine does.
 
Ok, so I have accomplished this and still nothing. I did this to get any kind of respone

If eventargs.commandline.count > 0 then
for each s as string in eventargs.commandline
msgbox(s)
next
end if

That only gives me a msgbox when I run the application after installing. The msg I get is "You have chosen to run ......... from ......."
THat's the only one I get.

I don't get ANY MSGS if I choose to start up the application by file.

My problem may be associating the file extension. Maybe I havn't followed the correct steps in this section.
 
If your app starts when you double-click a file then it is associated with that file extension and your will be being passed the file path as a commandline argument. If that wasn't the case then your app wouldn't be starting when you double-click the file. You're obviously not being completely clear because the code you just posted would not popup the message you mention. How about you be clear about exactly what code you're using (please use
VB.NET:
 tags too), exactly where it is located and exactly how you are starting the app.  Details are important and just because you know what you're doing doesn't mean that we do.
 
This is my code in the My.Application Class.


VB.NET:
[SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] OnStartup([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] eventArgs [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] eventArgs.CommandLine.Count > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] S [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] eventArgs.CommandLine
MsgBox(S)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].OnStartup(eventArgs)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]
 
Ok, thanks to you I have it all working now. It was pretty simple now that I finally understand what's happening. Thank you so much you helped me alot even though I was quite confusing. Thanks

I do however; have another question. Say for example my program is already running and i decide to double click another file of the same type. My app is single instance. Any ideas on how to change the file being used in the application to the newly found file?

Example: When listening to RealPlayer, if you goto My Music and double click another song this song starts in RealPlayer even if RealPlayer was already running. I want to accomplish the same thing.
 
You need to handle the StartupNextInstance event of the application. It will allow you to get the commandline arguments of the new instance and use them in the existing instance before the new instance is destroyed.
 
Back
Top