There is actually quite a bit that you need to do to accomplish something like this. There is a little tweaking of your application, and a little playing around in regedit. This turorial kind of goes hand-and-hand with my other tutorial "Read the arguments passes to your VB.NET application", but instead of making you read that one as well, i will only point out the important parts of it. First, make the modifications to your program as specified in this section of my other tutorial:

jacobjordan, on 9 Jul, 2008 - 07:03 PM, said:

There may be many times you want to have your application read the arguments (if any) that were passed to it. To do so, first add a new blank code file (if you don't have a template for a blank code file, you can just add a class and delete the code it puts in it) and call it "MyArguments.vb", or something like that. Once you have added that file, add this code in it:

Namespace My
Class MyApplication
#If _MyType = "WindowsForms" Then
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As ApplicationServices.StartupEventArgs) Handles Me.Startup
'*******************************************************
'* This is the sub that fires when your application starts. You can *
'* read the arguments from here. *
'*******************************************************
End Sub
'OnInitialize is used for advanced customization of the My Application Model (MyApplication).
'Startup code for your specific application should be placed in a Startup event handler.
<Global.System.Diagnostics.DebuggerStepThrough()> _
Protected Overrides Function OnInitialize(ByVal commandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean
Return MyBase.OnInitialize(commandLineArgs)
End Function
#End If
End Class
End Namespace

Look for the MyApplication_Startup() method in that code. That is the method that fires when your application starts, and that is also the method where you can read what arguments were passed to it.

Now, like said, you can read the arguments pass to your app from the MyApplication_Startup method. More specifically, use the

e.CommandLine

variable. That variable is a string collection, each member representing a different argument passed. Now, lets change modes a little bit, and go to actually changing the registry. For now, you have done all you can do in your application. Open Regedit, or Regedit32. If you don't know how, press Win+R, and type "regedit" in the space provided. Make sure your account permissions allow you to access that program, because making changes in it without knowing what your doing can have a very bad effect on your computer.
Now, once you are in regedit, you should see a tree view on the left side. Find the item that says "HKEY_CLASSES_ROOT" (it should be the topmost item). When you expand that item (it might take several seconds), you should see something like this:
Keep scrolling down, and you will see there is a key for every single file type listed on your computer. You will also see that near the bottom, there are keys with names like "txtfile", or "PaintShopProImage". Those keys are what i call "Definition Keys", i will explain why. Click on a normal key, one that has a name like ".txt" (I call them "Extension Keys"). Look at the "Default" value in the window.

(you should see something like this)
Now, look for a key name the same thing as the value of the (default) value in Classes Root. Once you do, expand it, and you should see something like this:
That is a picture of a "Definition Key". I call them that because they "Define" all the file types that reference them. Back to the .Txt example. The ".txt" registry key had a default value that pointed to another key in Classes Root, in this case it was "txtfile". The "txtfile" key contains information on all keys that refrense it, like ".txt". By Information, i mean the programs the type opens with, the description, the icon, and a few other things we won't get into. Go back to regedit and look at the txtfile key. There should be a subkey named "Shell". Expand that, and there should be a subkey of that called "Open" or "open". That key contains information of what program will open that file type when it is double-clicked. Expand the "Open" key and it should have a subkey called "command". select that key, and look at it's default value. It should have the path to a program, and a few arguments after it. For txtfile, it will look like this (in most cases):

%SystemRoot%\system32\NOTEPAD.EXE "%1"

Let's analyze that. The %SystemRoot% part is shorthand for "C:\WINDOWS" or "C:\WINNT", depending on what OS you have. So, "%SystemRoot%\system32\NOTEPAD.EXE", would actually be "C:\WINDOWS\system32\NOTEPAD.EXE", which is of course, the path to notepad. Now, the "%1" after that is an Argument. Arguments are seperated from the path of the program by spaces. If the path to your program has a space in it, enclosing it in quotes will prevent windows from counting it as separate arguments. "%1" is, like %SystemRoot%, shorthand. It represents the file that is to be opened. "%1" is also enclosed in quotes because the path to the file could likely have spaces in it. So, if you had a file called "C:\MyText.txt", and you wanted to open it, windows would look at these registry values and, ultimately, run notepad like this:

C:\WINDOWS\system32\NOTEPAD.EXE "C:\MyText.txt"

That would result in notepad opening up and displaying the contents of MyText.txt.

Back to the matter at hand, which is associating your program with one of those extensions. We will use ".txt" as an example. To associate your program with .txt extension, create a new subkey in Classes Root. Call it "Mytxtfile". It doesn't have to be that, but that is what we will use in this example. In that key, create a subkey called "Shell". In that, create a subkey called "Open". Finally, in that, create a subkey called "command". In the command key, right-click on the default value, and select "Modify". In that, type the path to your program (be sure to enclose it in quotes if it has a space in it), and " "%1"" after it. Make sure you enclose "%1" in quotes as well. So, if the path to your program was "C:\MyProgram.exe", you would have typed

"C:\MyProgram.exe" "%1"

Ok, i know this is a whole lot to understand. It took me weeks to learn it all. There's still one more thing you have to do, though. Remember the ".txt" extension key? Go to it and change it's default value to "Mytxtfile". Then, any time you double-click a .txt file, windows will run your program and pass it one argument: the name of the file that was double-clicked. Associating your program with file types is very complicated, and i am having a very hard time trying to explain it in a tutorial. I am almost sure you won't want to associate your program with text files, so to associate with something else like, a .jpg image file, you would still do the same thing. Create a key in Classes Root called "Myjpgfile" or something, and do the same process.

Wow, i've been typing at this thing for the past hour, and i could really use a break. I'm sure you could use a break too, so don't worry, were almost done. You have now made all the changes to the registry you need to, so back to your application. When your program runs, and the path to a file was passed to it, you no doubt want to do something with it in your main form. The only problem is, how will you let your main form know what file is to be opened? Here is the way i do it. Create a new module in your application. In that module, put something like this

Public FileToOpen As String

That variable can be accessed by both the MyApplication_Startup method and every form in your project. So, in the MyApplication_Startup method, put something like this

Then, when your main form starts up, you will be able to access what file is to be opened. If the FileToOpen variable contains nothing, then your program was run directly and no file is to be opened.

If you are planning to distribute your application, you will no doubt want to have your program to automatically associate itself with the necessary extensions. Using my explanation of file types in this tutorial, you should be able to incorporate something like that in your application.

Replies To: Associating your program with file types

1st ? = Does "FileToOpen" Need To Equal Anything. If It Does Will The Name Be Captured At Startup For me With the "MyApplication_StartUp" Method or Will I Have To Pre-Decide The File To Open

2nd ? = Let's Say I Captured The "FileToOpen" and It's Contents, How Would I Display Them In A Texbox.

3rd ? = I Know It's Possible To Change The Registry In VB.net. Would I Be Able To Edit The "Reg" For My Extension(.x21) In My Program. Like When The User First Runs It.

4th ? = I Read You're Other Tutorial ""Read the arguments passes to your VB.NET application" and I Was Curious(Cause I Didn't Get It Till I Read This Tutorial) What Other Stuff You Can Do By Reading The Command-Line Arguments At Startup. Also, If You Want To Tell Me, Where Did You Learn This Stuff.

First, when you declare FileToOpen in the module, it does not need to be assigned a value. It is assigned in the MyApplication_Startup method. You do not have to pre-decide the file to open before you run the application, i don't think you quite understand what FileToOpen is. FileToOpen will hold the path to the file the user is trying to open with your program. If a user double-clicked on a .x21 file, and the registry was set up right, it would launch your program and pass it a commmand line parameter with the path to that file. Then, in the MyApplication_Startup method, it is doing "Module1.FileToOpen = e.CommandLine(0)", setting FileToOpen to the file that is double-clicked on.

From your main form, if you had FileToOpen captured right and everythign and you wanted to show the contents of FileToOpen in a text box, you could use the System.IO.File.ReadAllText() method like "System.IO.File.ReadAllText(Module1.FileToOpen)". That would return the text contained in FileToOpen.

I'm not sure i quite understand #3.

It's been a while since i've used this in VB.NET, so off the top of my head i don't really know everything you can do in MyApplication_Startup methods. I don't want to be rude, but i'm kinda busy right now, and i don't really have time to open up VB 2008 and play around. Let's see, where did i learn all this? I learned it from a lot of different places, and by just figuring it out myself.