Click here to Skip to main content
11,409,450 members (49,232 online)
Click here to Skip to main content

File Association in VB.NET

, 11 Aug 2010 CDDL
Rate this:
Please Sign up or sign in to vote.
Easily associate your programs with file types (.jpg, .html, .mp3) with just 2 lines of Visual Basic code
Screenshot - VBFileAssociation1.gif

Ever wanted to have your application open when the user opens a certain type of file? This is accomplished fairly easily by editing just a couple of registry keys.

How are Programs Associated?

The registry stores all the file type-app associations. Click on the Start Menu > Run > Type in 'regedit' > Ok. Now expand the HKEY_CLASSES_ROOT node. At the top of the window are all the extensions that your computer recognizes. Scroll down to .txt and click on it. Now look at the default value, it probably is 'txtfile'. Scrolling down the tree on the left, find the txtfile node. This contains all the information about any extensions that have their default value set to txtfile. Right now, we're just interested in opening the file, so open Shell > Open > Command. If your .txt files open with Notepad, then the default value should be "%SystemRoot%\system32\NOTEPAD.EXE %1".
%SystemRoot% is pretty self-explanatory, it's replaced by the folder that contains system32, which contains NOTEPAD.EXE.
%1 is a command-line argument to pass the program when a txtfile is opened. %1 is replaced by the file's location.

Step 1: Running Your Program When a .hello File is Opened

The first step is to get your application to open when a chosen extension (like .mp3) is double clicked in Windows Explorer. For this article, we'll use a file extension that shouldn't exist: .hello. To use this file type, create a new project called 'Hello World'. The idea in the included project article is this: a .hello file contains (in plain text) an adjective. When one is opened, a message box will pop-up and say, "Hello, (file contents) World". If you open the application manually, it will associate itself with the .hello extension. Therefore, before .hello files will be opened with the demo program by default, you must run it on its own.

Now we have to edit the registry just like you saw with the .txt extension. Use the following code to do this:

	("", "Hello", Microsoft.Win32.RegistryValueKind.String)
	("Hello\shell\open\command").SetValue("", Application.ExecutablePath & _
	" ""%l"" ", Microsoft.Win32.RegistryValueKind.String)

What does all this do? If you don't understand My.Computer.Registry, here's a link to it on MSDN, or look below:

Code What it does
CreateSubKey(".hello") Creates a registry key in ClassesRoot for the .hello extension. Note that you must include the beginning period.
.SetValue("", "Hello"...
  1. "" (Or Nothing) sets the default value of the key.
  2. "Hello" is like the "txtfile" we saw earlier, it tells which registry key contains the information about the .hello extension.
CreateSubKey("Hello" & _ "\shell\open\command") This creates the "Hello" sub-key and the "store\open\command" subkey that is needed to store the path to the application that will open this file type.
.SetValue("", Application.ExecutablePath & _ " ""%l"" ",...
  1. Again, "" tells the application to set the key's default value.
  2. Application.ExecutablePath tells the code to associate the currently running executable with this file type.
  3. " ""%1"" " passes the opened file's location to your program. The quotes around it are optional, but if you have more than one argument, you must put them around each.

Now run your application once. It will edit the registry. Your program is now associated with the .hello file!

Now let's say you want to create a file association for .txt in your program. You create the file association, but it still opens in Notepad. What's going on? There is another value that needs to be deleted located here:


The value name is 'Progid'. This will consist of a string value of the default program to open this filetype. If this value is present, you will not be able to associate anything with this particular filetype. You must delete the 'Progid' value in order for the association to work.

Thanks, Computer Masster!

Now to test it out. Open Notepad, type in an adjective, and save it as a .hello file (Make sure you don't accidentally save it as a .hello.txt file). Open the file in Windows Explorer. Your program will run! But nothing happens...

Step 2: Reading the File Contents

When the registry is set correctly and a file is opened in Windows Explorer, the file's path is passed as a command-line argument (if you think of an application as a function, then these are the parameters). To retrieve the arguments, you use My.Application.CommandlineArgs. It returns a ReadOnlyCollection(Of String). You can use My.Application.CommandlineArgs(0) to retrieve the file path.

Now, we have to display the message. More code for the Load event:

  msgbox("Hello, " & My.Computer.FileSystem.ReadAllText_
	(My.Application.CommandlineArgs(0)) & " World!")

Screenshot - VBFileAssociation2.gif


Date Change
4/29/07 Article Submitted
  1. Fixed up article so it fits on one page
  2. Updated demo project
  1. Explained My.Application.CommandLineArgs
6/3/07 Added this tip
6/8/07 Fixed problems in the DLL
6/26/07 Changed the wordings in some phrases and fixed some errors in the examples
  1. Fixed links
  2. No more sidescrolling!
6/19/09 Removed buggy library
  • Rewrote the sample applications so that they do not rely on the removed library and are fully open source
  • Updated article


This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


About the Author

Nick Rioux

United States United States
No Biography provided

Comments and Discussions

QuestionHow to get type of file using with OpenDialog..? PinmemberMember 1060917817-Mar-15 23:39 
QuestionPermissions Nightmare Pinmemberjemlay12-Dec-14 7:24 
QuestionMy.Computer.FileSystem.ReadAllText(My.Application.CommandLineArgs(0)) PinmemberAmr Moussa27-Jul-13 18:44 
AnswerRe: My.Computer.FileSystem.ReadAllText(My.Application.CommandLineArgs(0)) PinmemberAmr Moussa27-Jul-13 19:55 
QuestionHelp me PinmemberMg Danger25-Jun-13 17:56 
QuestionExcellent!VOTE OF 5000! PinmemberGlobin18-Dec-12 7:22 
GeneralThanks! Pinmemberjanyk26-Oct-12 3:51 
GeneralBrilliant Pinmemberron_x27-Sep-12 3:14 
GeneralMy vote of 5 PinmemberGun Gun Febrianza12-Aug-12 6:55 
QuestionCool but i want to open an excisting file type with my program PinmemberKorlando9-Jun-12 5:10 
AnswerRe: Cool but i want to open an excisting file type with my program PinmemberGlobin18-Dec-12 7:40 
QuestionSome additional questions... PinmemberMember 81172656-Feb-12 13:19 
GeneralMy vote of 5 PinmemberNasenbaaer30-Jan-12 7:04 
GeneralMy vote of 5 Pinmemberaaroncampf1-Jul-11 8:33 
GeneralMy vote of 4 Pinmemberstefangab9522-Jun-11 22:26 
QuestionCool But How do i set the icon for the file that opens? PinmemberXtanX16-Oct-10 3:48 
AnswerRe: Cool But How do i set the icon for the file that opens? PinmemberIVAN PADRON14-Jun-11 9:58 
AnswerRe: Cool But How do i set the icon for the file that opens? PinmemberAmr Moussa27-Jul-13 20:02 
GeneralSuggestion for next article. PinmemberJalapeno Bob12-Aug-10 8:59 
Questionadding .dll PinmemberEmmery Chrisco10-Aug-10 11:31 
AnswerRe: adding .dll [modified] PinmemberNickr510-Aug-10 15:10 
GeneralRe: adding .dll PinmemberEmmery Chrisco10-Aug-10 18:01 
GeneralRe: adding .dll PinmemberNickr510-Aug-10 18:13 
GeneralMy vote of 2 Pinmemberjkljj22-Feb-10 8:45 
AnswerRe: My vote of 2 PinmemberNickr510-Aug-10 17:37 
GeneralMy vote of 2 PinmemberThePhilProject3-Jan-10 2:08 
General[My vote of 2] Souce code for all others: I can't belive you hid your code man, I gave this 5 for this, revoted to 2- Pinmemberadudley2564-Nov-09 23:32 
GeneralRe: [My vote of 2] Souce code for all others: I can't belive you hid your code man, I gave this 5 for this, revoted to 2- Pinmemberadudley2565-Nov-09 0:03 
GeneralRe: [My vote of 2] Souce code for all others: I can't belive you hid your code man, I gave this 5 for this, revoted to 2- PinmemberMr.Jinky14-Aug-10 4:38 
AnswerRe: [My vote of 2] Pinmemberadudley2564-Mar-11 3:57 
Generalsource code PinmemberDrKefir28-Oct-09 15:48 
GeneralMailto association Pinmemberasdfasdgf29-Jun-09 9:25 
Questionhow to remove association? PinmemberRathin Mina18-Mar-09 18:28 
GeneralNICE PinmemberMikeDaMan259414-Jan-09 16:56 
GeneralI can't get it working Pinmembergalaicra20-Apr-08 13:14 
GeneralRe: I can't get it working PinmemberNickr521-Apr-08 6:59 
GeneralBug at this version PinmemberAdiKL19-Nov-07 10:46 
GeneralCrusial Bug ! at this version PinmemberAdiKL19-Nov-07 10:44 
Questionerror PinmemberBetaNium11-Nov-07 6:18 
AnswerRe: error [modified] PinmemberNickr511-Nov-07 7:55 
Generalassociat more files PinmemberMrRAP20-Sep-07 9:17 
AnswerRe: associat more files PinmemberNickr520-Sep-07 9:45 
GeneralRe: associat more files PinmemberMrRAP21-Sep-07 4:47 
QuestionIcon PinmemberKschuler21-Aug-07 7:35 
AnswerRe: Icon PinmemberNickr521-Aug-07 15:39 
GeneralRe: Icon PinmemberKschuler22-Aug-07 3:59 
QuestionRe: Icon PinmemberKschuler29-Aug-07 11:31 
GeneralRe: Icon PinmemberNickr529-Aug-07 15:15 
GeneralRe: Icon PinmemberKschuler30-Aug-07 4:21 
GeneralRe: Icon [modified] PinmemberNickr531-Aug-07 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 11 Aug 2010
Article Copyright 2007 by Nick Rioux
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid