Understanding MS Windows File Associations

Archived content. No warranty is made as to technical accuracy. Content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

By Brien M. Posey, MCSE for TechRepublic.com


This article is courtesy of TechRepublic http://www.techrepublic.com/.

September 1999

One of the greatest features in Microsoft® Windows® 95, Windows 98, and Windows NT® is the way that Windows handles file associations. Simply open Windows Explorer and double-click on just about any file, and Windows will probably know what to do with it. For example, if you double-click a Microsoft® Word document, Windows will open Word and load the document that you selected. Many people think that this ability is due to some elaborate programming scheme within Word, but the capability is actually native to Windows. In this article, I'll show you exactly how file associations work and how to create your own file associations.

What Is a File Association?

In the introduction, I mentioned that within Windows Explorer, you could double-click a Microsoft Word document, and Windows would launch Microsoft Word and open the document. However, file associations actually go far beyond this simple task. Using the appropriate file associations, you can automate other tasks such as printing. You can even assign multiple tasks to a single file type.

However, before I begin that discussion, you need to understand what a file association really is. In MS-DOS®, every filename is made up of up to eight characters, a period, and a three character filename extension. This extension typically defines the type of file. For example, files ending in .com, .exe, and .bat are executables. In MS-DOS, if you type the name of a file that ends in one of these extensions, MS-DOS will attempt to execute the file. If you change the extension of an executable file, the file will still contain executable instructions. If you type the filename, MS-DOS won't attempt to execute the file because it doesn't recognize the filename's extensions as an executable file type.

Microsoft has kept this basic concept and built on it in Windows 9x and Windows NT. To see this concept in action, open Windows Explorer and go to the Windows directory. As you scroll through the list of files, notice that many files have specific types of icons next to them. For example, certain text files (such as Bind List Log.txt) have the Notepad icon next to the filename, as shown in Figure A.


Figure A: Certain text files have the Notepad icon beside them.

Now, select Folder Options from Windows Explorer's View menu. When you see the Folder Options properties sheet, select the View tab. At this point, make sure the Hide File Extensions For Known File Types check box is cleared, as shown in Figure B. Next, click Like Current Folder, then click OK.


Figure B: Make sure the Hide File Extensions For Known File Types check box is cleared.

Following these steps make all the filename extensions visible, because they're hidden by default. Now, when you look at your files in Windows Explorer, you'll notice that most of the filenames contain a period and a three-character extension like the files shown in Figure A.

I mentioned that many text files have the Notepad icon next to the filename. If you look closely at Figure A, you'll notice that the only files with the Notepad icon are files that have a filename with a .txt extension. If you double-click any of these files, Windows will open Notepad and attempt to load the file.

To further understand how filenames affect registered associations, go to your root directory and rename the file Config.bak (if it exists) to Config.txt. You can do this by right-clicking the filename and selecting Rename from the shortcut menu. When you rename the file, you'll see the file's icon instantly change to the Notepad icon. If you double-click the file, Windows will launch Notepad and display the contents of the file. Be sure to change the name of the file back to Config.bak when you're done experimenting. You should also avoid renaming any system file or any file if you're not sure of its function, or you could destroy Windows.

Shortcut Menus

Windows' ability to differentiate between file types doesn't end at its ability to open the appropriate application when you select a file. The Windows interface is capable of performing many other tasks based on file types.

To see these other abilities in action, open Windows Explorer and right-click a Microsoft Word document (a filename with a .doc extension). When you do, a shortcut menu will appear, as shown in Figure C.


Figure C: Shortcut menus contain unique options based on the file types.

The shortcut menu anticipates your actions based on the context of the type of file you're working with and offers the appropriate choices.

For example, some menu choices are universal throughout Windows. You always have the option of creating a shortcut, deleting a file, renaming a file, or viewing the file's properties. However, the menu options New, Print, and Open are specific to Microsoft Word files. Notice that the Open option is bold. This means that Open is the default action when you double-click the file. Naturally, the Open option instructs Windows to launch Microsoft Word and open the selected file. The Print option enables you to print the file and the New option enables you to create a new Microsoft Word document.

To illustrate how shortcut menus differ from each other, right-click a .wav file. When you do, you'll see a different type of shortcut menu. Because .wav files deal with sound, you shouldn't expect to see a print option. Instead, you'll see the options shown in Figure D, which enable you to open the file, play the file, or record a new file.


Figure D: Shortcut menu options are different for a .wav file than for a Microsoft Word document.

How Does Windows Know?

Now that you understand the true power of Windows file associations, the question remains: How does Windows know how to set the file associations? Before I answer this question, remember that each program uses a unique extension. When you install a program, the program embeds commands in the registry that tell Windows how to act when the user selects such a file.

You can easily view these settings by opening the Registry Editor. To do so, select Run from the Start menu and type REGEDIT.

Warning: Before continuing, I should advise you that the registry is the backbone of Windows. Don't make changes to the registry, or you could destroy Windows or other programs. If you decide to try the steps outlined in this section, you should back up your computer before doing so.

Open the Registry Editor and navigate to HKEY_CLASSES_ROOT|.doc. When you do, you'll see that .doc is an extension associated with the MSWord application, as shown in Figure E. You can also see in the figure that there are several other branches beneath .doc.


Figure E: .Doc is an extension associated with the Microsoft Word application.

As you can see, this section identifies the file type. Now, navigate to HKEY_CLASSES_ROOT|Word.Document.8|shell. This key may vary depending on your version of Microsoft Office. However, as you can see in Figure F, the shortcut menu options appear beneath this part of the Registry.


Figure F: The HKEY_CLASSES_ROOT|Word.Document.8|shell key of the Registry contains the shortcut menu options that are unique to Microsoft Word.

If you expand each of these context menu commands, you'll see that each contains a key called COMMAND and a key called DDEEXEC. The COMMAND key is used for simple commands that are specific to that application only. The DDEEXEC section contains commands relating to dynamic data exchange. Dynamic data exchange refers to some programs' ability to share information between programs. For example, you can cut part of a Microsoft Excel spreadsheet and paste it into a Microsoft Word document. The spreadsheet will continue to behave like a spreadsheet because of the way that Microsoft Office makes use of dynamic data exchange.

Creating Your Own File Associations

After looking at all of the Registry entries responsible for the behavior of file associations, you may think that file associations are extremely complex to establish. From a developer's standpoint, that statement may be true. However, anyone can manually set up file associations with little difficulty. If you're a developer and plan to set up an association through a program, you should be aware that there are many more Registry keys involved than the ones I've pointed out.

Let's say you're a system administrator and your in-house programming staff writes a custom application that deals with a unique file type. You can easily associate the new file type with the application so that if a user clicked on a file created by the application, Windows would launch the application and open the file.

To create your own file associations manually, begin by opening Windows Explorer. At this point, select Folder Options from the View menu. When you do, you'll see the Folder Options properties sheet. Now, select the File Types tab. When you do, you'll see a screen similar to the one shown in Figure G.


Figure G: The File Types tab enables you to view and edit registered file types.

Notice this tab displays all of the registered file types on your system. It also contains New Type, Remove, and Edit buttons. To see how this screen really works, select Bitmap Image from the Registered File Types list box and click Edit. When you do, you'll see the Edit File Type dialog box.

Before continuing, let's take a look at the anatomy of this dialog box. As you can see in Figure H, the dialog box contains a description of the file type, the registered extension, and the various shortcut menu options.


Figure H: The Edit File Type dialog box contains a description of the file type, the registered extension, and the various shortcut menu options.

Note that that Open is bold, indicating that it's the default action. Select Open and click Edit. As you can see in Figure I, the Open action uses the command C:\PROGRA1\ACCESS1\MSPAINT.EXE" "%1.

If you're familiar with MS-DOS batch file programming, you'll recognize the style of this command. In MS-DOS batch files, the percent file followed by a number indicates that a parameter specified within the command should be appended to the command.

In this case, that parameter is the name of the file that you've selected. For example, if you had selected a file called Photo.bmp, the command would be interpreted as C:\PROGRA1\ACCESS1\MSPAINT.EXE PHOTO.BMP. In MS-DOS, such a command would launch the MSPaint.exe program and attempt to open the file Photo.bmp (assuming that MSPaint.exe was written to allow command line parameters). Notice in Figure I that if you want to use DDE (dynamic data exchange), you can do so by selecting the Use DDE check box. When you do, you'll see the dialog box expand to look like the one shown in Figure J. (Further detail on DDE is beyond the scope of this article.)


Figure I: You can view the commands behind the various context menu options.


Figure J: You can also use DDE commands with file associations.

Now that you've seen the way shortcut menu commands work, it's time to create your own file association. To do so, return to the File Types tab of the Folder Options properties sheet and click New Type. When you do, you'll see the Add New File Type dialog box.

Now, provide all of the requested information, but don't add actions to the file type just yet. You can see an example of what the dialog box should look like in Figure K. Remember that to set up an association, you must use an extension that no other program is already using. Any information you enter in the Add New File Type dialog box will become associated with the filename extension that you specify. You can even associate an icon with the extension by clicking Change Icon.


Figure K: Enter the appropriate information for the new extension.

Now it's time to set up a default action and any shortcut menu options that you want to associate with the file type. To do so, click New. When you do, you'll see a dialog box asking for the name of the action and the command behind it. If you're unsure of the appropriate command, you can usually figure it out by looking at other file types. When you're done, select the action that you wish to be the default action. If it isn't already bold, click Set Default to make it the default. Click OK to finalize your changes.

When you close all of the open dialog boxes, you can test your new file type by creating a file with the registered extension. Windows should assign the file the icon that you associated with the filename's extension. If you right-click the file, you should see the appropriate shortcut menu options. Finally, double-click the file and the program that you've associated with it should attempt to open the file.

Brien M. Posey is an MCSE and a freelance technical writer. He works as the director of information systems for a large healthcare company. Brien has also worked as a network engineer for the Department of Defense. You can contact him at Brien_Posey@xpressions.com. (Because of the large volume of e-mail he receives, it's impossible for him to respond to every message. However, he does read them all.)

The above article is courtesy of TechRepublic http://www.techrepublic.com/.

We at Microsoft Corporation hope that the information in this work is valuable to you. Your use of the information contained in this work, however, is at your sole risk. All information in this work is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Microsoft Corporation. Microsoft Corporation shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages. All prices for products mentioned in this document are subject to change without notice.

International rights.