Windows tips

How to create a file association for your programs, using the registry

First, what is a file association? You can see it when you double-click in the Windows Explorer on a file whose extension is known by the system (eg.: .doc, .wri, .txt, etc.). The Explorer will then open it with its associated program, Word, WordPad, NotePad, etc.
You can also right-click on the file, or open the Explorer File menu, and choose an item in the displayed context menu. Usually, they include Open, Edit and Print. Explorer will open the program associated to the item, which will perform the requested operation.

Well, I see a question coming here: "What is the difference between Open and Edit?".
For a .doc or a .txt file, usually none, that's why there is no Edit choice for them. But for an .inf, .bat or .html file, among others, there is a slight difference: Open executes the file (install a program for .inf, run MS-Dos commands for .bat) or renders it (display the HTML page for .html), while Edit will open it in a text editor (for example) so you can edit the code.

You can see the current file associations by opening Explorer Options, then going to the File Types tab. (exact procedure depends if IE4/5 is installed or not). The dialog box displays all the registred extensions by their names (as displayed in the Type column in the Detailled view of Explorer). If you click on one name, it displays the extensions covered by this type, and the default associated program. You can modify some settings, but it seems quite limited (don't allow some edits, and actually don't work correctly when adding a new type), so the best way to do it is to tweak the registry, as explained below.

Now, how Explorer knows which program is associated to a file extension?

[[[To be continued...]]]

Old version:

Run RegEdit, go to the HKEY_CLASSES_ROOT key, and choose a subkey, like .wri.
Export it to a file, it will look like:
REGEDIT4

[HKEY_CLASSES_ROOT\.wri]
@="wrifile"
Search, still in the HKEY_CLASSES_ROOT key, the wrifile subkey (hint: type quickly "wri", you should be on it).
Export it in another file.
It will look like:
REGEDIT4

[HKEY_CLASSES_ROOT\wrifile]
@="Write Document"    ; Text displayed in the Type column in Explorer

[HKEY_CLASSES_ROOT\wrifile\DefaultIcon]
@="C:\\Progra~1\\Access~1\\WORDPAD.EXE,2"    ; Icon displayed by Explorer

[HKEY_CLASSES_ROOT\wrifile\CLSID]
@="{73FDDC80-AEA9-101A-98A7-00AA00374959}"

[HKEY_CLASSES_ROOT\wrifile\shell]

[HKEY_CLASSES_ROOT\wrifile\shell\open]

[HKEY_CLASSES_ROOT\wrifile\shell\open\command]
@="C:\\Progra~1\\Access~1\\WORDPAD.EXE \"%1\""

[HKEY_CLASSES_ROOT\wrifile\shell\print]

[HKEY_CLASSES_ROOT\wrifile\shell\print\command]
@="C:\\Progra~1\\Access~1\\WORDPAD.EXE /p \"%1\""

[HKEY_CLASSES_ROOT\wrifile\shell\printto]

[HKEY_CLASSES_ROOT\wrifile\shell\printto\command]
@="C:\\Progra~1\\Access~1\\WORDPAD.EXE /pt \"%1\" \"%2\" \"%3\" \"%4\" "
Ignore the CLSID, specific to Write. You can also ignore the DefaultIcon, though it is useful (without it, Windows will show the default icon for unknow file).
The interesting part is the shell one.

You can see here, there are three commands (verbs) associated with a .wri file: open, print and printto.
And the default entry for each "command" subkey is the path of the program (make sure to enclose it with double quotes (preceded by a '\' char.) if you type the long path including spaces), followed by the command line parameters (also enclosed in double quotes, in case of spaces in these parameters).
Of course, if you create such an entry for your program, it must read these parameters with the Command function (for VB) or from the lpszCmdLine parameter of the WinMain routine (for C).

Notice that in the shell menu, the standard (canonical) verbs are translated in the language Windows run on. Ie. on my French Windows, I will have "Ouvrir" and "Imprimer" as menu items.
But you can also create your own verbs, with the text as it will appears in the menu.
For example:

[HKEY_CLASSES_ROOT\cfile\shell\Open With MyProg]
@="Edit With &MyProg"    ; Menu text -- shell uses the key name if there is no default key string.

[HKEY_CLASSES_ROOT\cfile\shell\Open With MyProg\command]
@="\"C:\\PROGRAM FILES\\My Company\\My Prog\\MyProg.EXE\" \"%1\""
The good thing is: the default verbs are still available, eg. a bat file will still execute, an inf file will still install, etc. It is a lot less intrusive than overwriting an existing key. Or keep the overwritten key and restore it if your program is uninstalled, but better ask the user first...
I use this method to open the various C and VB files with my favorite editor, and keeping the possibility to open them with Visual Studio (MSDev).
Note that if you want your program to be the default one (when double-clicking on the associated file), it must be recorded first in the registry. To do that, I delete the previous default one, I put mine, and I put back the previous one (I exported it before). Unfortunately, I don't know a way to delete a registry key from a reg file. If anyone know? So you have to do it manually.

Note that you can do all this by editing the file associations with the Explorer provided dialog. But the advantage of reg file is that it allow to do this quickly on a number of computers (or to restore them after reinstalling Windows).

If you create a new file extension, merge the two reg files, replace .wri with your extension, wrifile with any name not already existing, and the Write path with the path of your application. That's all (well, edit out the keys you don't want, of course).

Related doc. to format and improve...

List of canonical verbs:
open		command to open the file
print		command to print the file
explore		? specific to directory / folder
find		? specific to directory / folder
openas		? command to open the file in a special way
properties	? command to display the file properties
printto		! Not displayed in context menu. It allows the user to print a file by dragging it to a printer object.

Typical printto command:
C:\MyDir\MyProgram.exe /p %1 %2 %3 %4
In this example, %1 represents the file name and %2 the printer name.
You can ignore %3 and %4 for Windows 95 and later systems.
For Windows 3.1 systems, %3 represents the driver name and %4 the port name.

Optionally, you can define a default verb for the file class by putting its name in the default (string) value of the shell subkey.
Note that this doesn't work if the very have spaces in its name:
"Open With The Editor" is a valid verb (used in the context menu) but you can't make it default, unless you rename it
"OpenWithTheEditor", or something like that.
The default verb is displayed first on the context menu, in bold characters, and executed when you double-click on the item.
Its purpose is to provide the shell with a verb it can use when ShellExecuteEx is called, but no verb is specified.
The shell does not necessarily select the default verb when ShellExecuteEx is used in this fashion.
For shell versions 5.0 and later, found on Windows 2000 and later systems, the shell uses the first available verb from the following list.
If none are available, the operation fails.

The open verb
The default verb
The first verb in the registry (in shell versions 5.0 and later only)
The openwith verb

The first verb in the registry is the first to be written, not necessarily the order displayed by RegEdit.

You can create a ShellNew subkey for your file class (xxxfile) to add this file type in the shell New command (in File / New)
or in the New directory context menu item.
Create this ShellNew subkey under the file extension (.xxx) in HKCR, and create one of the following values:
Command		Executes an application. This is an REG_SZ value specifying the path of the application to be executed. For example, you could set it to launch a wizard.
Data		Creates a file containing specified data. Data can be either a REG_SZ or REG_BINARY value with the file's data. Data is ignored if either NullFile or FileName are specified.
FileName	Creates a file that is a copy of a specified file. FileName is a REG_SZ value, set to the fully qualified path of the file to be copied.
NullFile	Creates an empty file. NullFile don't have a value. If NullFile is specified, Data and FileName are ignored.

The Open With context menu appears when the user right-clicks on a file whose extension isn't in the registry
(the file is no member of a class) or when the user shift-right-clicks on any file.
It opens a dialog that also appears when double-clicking on such a file.
This dialog lists all the applications that are listed under the HKCR\Applications subkey,
unless the application's subkey have a NoOpenWith REG_SZ empty value, or is listed in the
the HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\FileAssociation value named KillList.
This value lists the program names, separated by semicolons (;).

If asked, this dialog will always open files with this extension with the given application, creating two entries in the registry:
HKCR\.xxx (if .xxx is the previously unknown file extension)
HKCR\xxx_auto_file with the given description and a shell\command with the choosen application.

A message to SciTE list, to format and improve too...

> I am looking for a fast and convenient way to associate ascii files (eg:
> *.cpp, *.aspx, *.txt, *.rc, etc...) to SciTE.
>
> Am i the only one looking for such an automatic operation ?
>
> Can you please let me know if there is any easy way to register many
> file types to SciTE ?
>
> I was thinking, maybe a batch file script would do. But since i'm not an
> expert at manually editing the windows registry, i was wondering maybe
> there are some people who have already done such scripts (and are
> willing to share them to some newbies ;p).

No batch file, a simple .reg file can do the job, ie. an export of registry keys/values
that can be imported back after changes.

On Windows 98/NT, I used the following file to associate SciTE to *all* filetypes, Ascii or binary.
This is possible because SciTE can handle most binary types and I have made a test (max.file.size)
to avoid loading accidentally files too big...

-----8<-----
REGEDIT4

[HKEY_CLASSES_ROOT\*\shell\edit]
@="Edit with &SciTE"

[HKEY_CLASSES_ROOT\*\shell\edit\command]
; Adapt path to your...
@="\"C:\\Program Files\\UTexts\\SciTE\\SciTE.exe\" \"%1\""

[HKEY_CLASSES_ROOT\*\shell\print\command]
; Optional, of course
@="\"C:\\Program Files\\UTexts\\SciTE\\SciTE.exe\" /p \"%1\""
----->8-----

Note that files without preset association (eg. .xyz) are opened in SciTE when double-clicking
on them, while those with existing association retain their default open program,
"Edit with SciTE" is still in the context menu, but you have to choose it explicitely when
right-clicking on them.

Now, I use my own program, ProgLauncher, to replace notepad.exe and thus  automatically
open in SciTE all files associated to Notepad.

Alas, both tricks fails in Windows XP. For some reason, SciTE appears only in the
"Open With" menu, and not every time. And I failed to replace Notepad because
WinXP SP 2 resists to most tricks allowing to replace a system file:
it "autorepairs" Notepad whenever I replace it.
And since the XP archive is on D disk, the trick stating to ignore the plea to insert the XP CD
doesn't work...

Anyway, you can quickly make your associations, with or without overriding the existing ones.
See above...

In short, make a .reg file with the following lines:

-----8<-----
[HKEY_CLASSES_ROOT\cfile\shell]
; Default value (double-clik & bold menu item).
; If not there, default value is first (chronologically)
; entered shell verb (Windows/already installed prog)
@="EditWithSciTE"

[HKEY_CLASSES_ROOT\cfile\shell\EditWithSciTE]
; Displayed context menu entry
@="Edit with &SciTE"

[HKEY_CLASSES_ROOT\cfile\shell\EditWithSciTE\command]
; Update path...
@="\"C:\\Program Files\\UTexts\\SciTE\\SciTE.exe\" \"%1\""
----->8-----

and duplicate these lines, replacing cfile with whatever name you want to associate to:
textfile, hppfile, etc.

This allow to keep the original file associations, eg. DevStudio ones, or IE/Firefox for HTML files, etc.

To exclusively open with SciTE, just override the open verb:

-----8<-----
[HKEY_CLASSES_ROOT\cfile\shell\open\command]
; Update path...
@="\"C:\\Program Files\\UTexts\\SciTE\\SciTE.exe\" \"%1\""
----->8-----

Go to main page

Contact me, comment this

Created: 1999/06/03
Updated: 2005/08/15 (Added message to SciTE)