Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / Windows Forms

File Association in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.06/5 (39 votes)
11 Aug 2010CDDL4 min read 353.5K   6.4K   131   76
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:

VB.NET
My.Computer.Registry.ClassesRoot.CreateSubKey(".hello").SetValue_
	("", "Hello", Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey_
	("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:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt

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:

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

Screenshot - VBFileAssociation2.gif

History

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

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMailto association Pin
asdfasdgf29-Jun-09 8:25
asdfasdgf29-Jun-09 8:25 
Questionhow to remove association? Pin
Rathin Mina18-Mar-09 17:28
Rathin Mina18-Mar-09 17:28 
GeneralNICE Pin
MikeMatt1614-Jan-09 15:56
MikeMatt1614-Jan-09 15:56 
GeneralI can't get it working Pin
galaicra20-Apr-08 12:14
galaicra20-Apr-08 12:14 
GeneralRe: I can't get it working Pin
Nick Rioux21-Apr-08 5:59
Nick Rioux21-Apr-08 5:59 
GeneralBug at this version Pin
AdiKL19-Nov-07 9:46
AdiKL19-Nov-07 9:46 
GeneralCrusial Bug ! at this version Pin
AdiKL19-Nov-07 9:44
AdiKL19-Nov-07 9:44 
Questionerror Pin
BetaNium11-Nov-07 5:18
BetaNium11-Nov-07 5:18 
I get this error when I run my program: Value cannot be null.
Parameter name: path .

It's in this line:
MsgBox("Hello, " & My.Computer.FileSystem.ReadAllText(DoAssociation.WriteCommandsToArray(My.Application.CommandLineArgs)(0)) & " World!").

What does that mean?Confused | :confused:


This is my signature.

AnswerRe: error [modified] Pin
Nick Rioux11-Nov-07 6:55
Nick Rioux11-Nov-07 6:55 
Generalassociat more files Pin
MrRAP20-Sep-07 8:17
MrRAP20-Sep-07 8:17 
AnswerRe: associat more files Pin
Nick Rioux20-Sep-07 8:45
Nick Rioux20-Sep-07 8:45 
GeneralRe: associat more files Pin
MrRAP21-Sep-07 3:47
MrRAP21-Sep-07 3:47 
QuestionIcon Pin
Kschuler21-Aug-07 6:35
Kschuler21-Aug-07 6:35 
AnswerRe: Icon Pin
Nick Rioux21-Aug-07 14:39
Nick Rioux21-Aug-07 14:39 
GeneralRe: Icon Pin
Kschuler22-Aug-07 2:59
Kschuler22-Aug-07 2:59 
QuestionRe: Icon Pin
Kschuler29-Aug-07 10:31
Kschuler29-Aug-07 10:31 
GeneralRe: Icon Pin
Nick Rioux29-Aug-07 14:15
Nick Rioux29-Aug-07 14:15 
GeneralRe: Icon Pin
Kschuler30-Aug-07 3:21
Kschuler30-Aug-07 3:21 
GeneralRe: Icon [modified] Pin
Nick Rioux31-Aug-07 1:23
Nick Rioux31-Aug-07 1:23 
GeneralRe: Icon Pin
Kschuler4-Sep-07 2:55
Kschuler4-Sep-07 2:55 
GeneralRe: Icon [modified] Pin
Nick Rioux4-Sep-07 4:30
Nick Rioux4-Sep-07 4:30 
GeneralRe: Icon [modified] Pin
Kschuler10-Nov-11 7:40
Kschuler10-Nov-11 7:40 
AnswerRe: Icon Pin
Member 44102718-Jan-09 10:42
Member 44102718-Jan-09 10:42 
Question"Open With" if the program is already running Pin
Schadenfroh28-Jun-07 12:17
Schadenfroh28-Jun-07 12:17 
AnswerRe: "Open With" if the program is already running Pin
Nick Rioux29-Jun-07 2:02
Nick Rioux29-Jun-07 2:02 

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

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