A user on the VB Development Forum I’ve visited for a few years asked a question about creating file associations. I chuckled and assumed that a Google search would return a wealth of information about the subject. I was wrong; by my fifth combination of search terms I had found one site that had the minimal steps to get it done, and it wasn’t even correct. The rest of the results were happy to discuss using the File Types tab of the Folder Options dialog to accomplish the task; this is not programmatic and not what the user wanted. I remembered having a heck of a time trying to figure out how to make an association for all files (so I could have a right-click->Edit with hex editor option) so I decided I’d write up what I know. Shame on you, internet, for letting the information get buried.
To start off, my example is technically incorrect. I’m working with HKCR, but apparently the content here is generated based off of HKCU\Software
and HKLM\Software
. I assume this is so you can have per-user and global associations; I wrote my example using HKCR directly but you should probably use one of these two; please understand when I say HKCR I mean one of the other two locations.
I found the File Associations documentation, but haven’t had time to look over it properly. For now, this should do. In order to make it so that when you double-click a file your application opens you need three things:
- Your application should support opening a file from the command-line in some way.
- You need a registry key to associate a file type with the extension.
- You need a registry key to associate a program with the file type.
The first one is up to you; I can’t tell you how to do it. The registry key to associate a file type with an extension is pretty easy. File extensions are listed in HKCR under keys named after the extension (including the period!) The value of these keys is a string that defines the file type associated with the extension. I tend to not use spaces in these, but I have no idea if spaces are unsupported; it will end up being the name of a subkey later and I’m too lazy to check if spaces are supported in subkey names. For example, text files have the extension txt, and the key that specifies the file type is HKCR\.txt. It’s default value is txtfile.
Now that there’s a file type associated with the extension, you need a registry key that tells the shell how to handle operations on the file type. There’s lots of things to implement here, but I only know the basics. File types are declared in subkeys of HKCR named after the file type; their subkeys and default values vary based on exactly how the file type is to be handled. All I’m going to discuss is handling the "open" verb, which is generally invoked by double-clicking a file in the shell. To add support for this verb, a tree of subkeys need to be created under HKCR\filetype. Working with our Notepad example above, we see HKCR\txtfile\shell\open\command. It has a value that represents the path to Notepad and the replacement string %1 for the file that was selected.
Example time
Suppose we want to associate Notepad with the file extension .zzz. We’ll start by defining a key for the file type:
HKCR\.zzz
Value="MyEditorFile"
Now, we create the file type key and associate the "Open" verb with notepad:
HKCR\MyEditorFile\open\shell\command
Value="%SystemRoot%\system32\NOTEPAD.EXE %1"
Note that though there are no quotes in the value for the open key, I’ve had trouble in the past when this string didn’t have anything that could possibly contain a space quoted. For example, I’m used to using: "C:\Program Files\My Program\program.exe" "%1"
I’m not sure why Notepad’s special and doesn’t require this. That’s really all I have to say on this topic for now; I am still looking over the documentation.