Creating a File Association
--------------------------------------------------------------------------------
Files that contain a particular type of data commonly have the same file name extension. It is appended to the file name and typically consists of a period followed by three alphanumeric characters. For example, ANSI text files commonly have a .txt file name extension.
Although it is customary, file name extensions are not restricted to three letters on systems that support long file names. On Microsoft® Windows® 95 and later systems, you can use any number of characters you like as long as the file name doesn't exceed 255 characters.
Note You can use multiple periods in a file name, but only those characters following the final period are recognized as a file name extension. Any other periods are treated as part of the file name. Although file names can contain spaces, do not use spaces in file name extensions.
Any time you create or change a file association, it is important to notify the system that you have made a change with SHChangeNotify, specifying the SHCNE_ASSOCCHANGED event. If you do not call SHChangeNotify, the change might not be recognized until the system is rebooted.
Defining a File Class
Defining Attributes for a File Class
Registering an Application to Handle Arbitrary File Classes
Excluding an Application from the Open With Dialog Box
Defining a File Class
Files with a common file name extension can be defined as members of a file class. Defining a file class allows you to extend the Shell by customizing the behavior of all files in the class. The Shell Basics: Extending the Shell section discusses those behaviors that can be customized by adding registry entries or special files, including:
Specifying the application used to open the file when it is double-clicked.
Adding commands to the shortcut menu.
Specifying a custom icon.
For a greater degree of control over the behavior of a file class, you can write one or more Creating Shell Extension Handlers.
To define a file class, first create a registry key for the extension, including the period, under HKEY_CLASSES_ROOT . Set the key's value to the ProgID for the associated application. Next, create a second key under HKEY_CLASSES_ROOT for the application's ProgID. Set it to a REG_SZ value that describes the application. For example, to create a file class with an .myp extension and an associated application, MyProgram.exe with a ProgID of MyProgram.1, the registry entries would be as follows.
HKEY_CLASSES_ROOT
.myp
(Default) = MyProgram.1
MyProgram.1
(Default) = MyProgram Application
A user can act on a member of the file class in a variety of ways, such as double-clicking or right-clicking the file in Windows Explorer. Once these two keys are in place, you can add subkeys to them to customize the behavior of the file class and its associated application. When a user acts on a member of the class, the Shell's response includes the information contained in these keys.
Defining Attributes for a File Class
Assigning attributes to a file class allows you to control some aspects of its behavior. It also allows you to limit the extent to which the user can modify various aspects of the class, such as its icon or verbs, with the Folder Options property sheet. The attributes are defined as binary flags. To assign attributes to a file class, combine the selected attributes with a logical OR to form a single attribute value. Add an EditFlags REG_BINARY value to the class's ProgID key and set it to the attribute value. The following table lists the file class attributes and their numerical values.
Flag Value Description
FTA_Exclude 0x00000001 Exclude the file class.
FTA_Show 0x00000002 Show file classes, such as folders, that aren't associated with a file name extension.
FTA_HasExtension 0x00000004 The file class has a file name extension.
FTA_NoEdit 0x00000008 The registry entries associated with this file class cannot be edited. New entries cannot be added and existing entries cannot be modified or deleted.
FTA_NoRemove 0x00000010 The registry entries associated with this file class cannot be deleted.
FTA_NoNewVerb 0x00000020 No new verbs can be added to the file class.
FTA_NoEditVerb 0x00000040 Canonical verbs such as open and print cannot be modified or deleted.
FTA_NoRemoveVerb 0x00000080 Canonical verbs such as open and print cannot be deleted.
FTA_NoEditDesc 0x00000100 The description of the file class cannot be modified or deleted.
FTA_NoEditIcon 0x00000200 The icon assigned to the file class cannot be modified or deleted.
FTA_NoEditDflt 0x00000400 The default verb cannot be modified.
FTA_NoEditVerbCmd 0x00000800 The commands associated with verbs cannot be modified.
FTA_NoEditVerbExe 0x00001000 Verbs cannot be modified or deleted.
FTA_NoDDE 0x00002000 The Dynamic Data Exchange (DDE)-related entries cannot be modified or deleted.
FTA_NoEditMIME 0x00008000 The content-type and default-extension entries cannot be modified or deleted.
FTA_OpenIsSafe 0x00010000 The file class's open verb can be safely invoked for downloaded files. Note that this flag may create a security risk, because downloaded files could contain malicious content. To reduce this risk, consider methods to scan downloaded files before opening.
FTA_AlwaysUnsafe 0x00020000 Do not allow the Never ask me check box to be enabled. The user can override this attribute through the File Type dialog box. This flag also affects ShellExecute, download dialogs, and any application making use of the AssocIsDangerous function.
FTA_AlwaysShowExt 0x00040000 Always show the file class's file name extension, even if the user has selected the Hide Extensions option.
FTA_NoRecentDocs 0x00100000 Don't add members of this file class to the Recent Documents folder.
The following example assigns the FTA_NoRemove (0x00000010) and FTA_NoNewVerb (0x00000020) attributes to the .myp file class.
HKEY_CLASSES_ROOT
.myp
(Default) = MyProgram.1
MyProgram.1
(Default) = MyProgram Application
EditFlags= 00 00 00 30
Registering an Application to Handle Arbitrary File Classes
The most common use of file classes is to associate a file name extension with the application that should be used to open members of the class. The user can then open a file by double-clicking the file icon or by selecting a command such as Open or Edit from the file's shortcut menu. Some applications can be associated with a single file class; other applications can be associated with many file classes. For a detailed discussion of how to implement this behavior, see Extending Shortcut Menus.
In some cases, however, the user might attempt to open a member of a file class with an application that is not associated with the class. For example, if the user double-clicks a file that is not associated with an application, an Open With dialog box appears that allows him or her to specify which application should open the file. The user can also use this dialog box to specify which application is associated with a particular file name extension. The following illustration shows a typical Open With dialog box.
In most cases, the application is asked to open a file that it is capable of handling. For example, many applications can handle .txt files. In other cases, the file might be completely incomprehensible. The ways that applications open arbitrary files can be divided into three general classes:
Class 1. Open only those file classes that have a registered association with the application. If the user attempts to have the application open any other file class, return an error. Applications of this type are typically designed to work only with certain proprietary file formats.
Class 2. Use the same procedure to open any file. Examine the contents of the file and display the file, if possible. Only return errors for those files that the application cannot handle. Applications of this sort are typically designed to work with a common file format, such as ANSI text, that can be the underlying format of many file classes.
Class 3. Have one set of procedures for opening files that have a registered association with the application. Provide a default procedure to open all other files. Applications of this sort are usually a hybrid of the first two classes. They have one or more proprietary formats that need special handling, but they are also capable of handling some standard formats.
Assume, for instance, that the MyProgram application discussed in the preceding sections of this document can also handle .txt files. If a user attempts to use the Open With dialog box to open a .txt file with MyProgram, or associate .txt files with MyProgram, the Shell attempts to open the file as if it were an .myp file. What happens next depends on how the application opens files. If MyProgram falls into either of the first two classes, there is no need for special handling by the Shell. For class 1, MyProgram would simply examine the file name, see that it lacked an .myp extension, and return an error. For class 2, MyProgram would use its standard procedure for opening all files. However, with Windows 2000, the Shell can be extended to make class 3 applications easier to implement as well as provide two important new features for users.
What makes the third class of applications more difficult is that, if MyProgram is expecting a proprietary .myp file, it might have problems handling a .txt file. On the other hand, writing a general-purpose routine to open all files the same way can be inefficient. Instead, the Shell allows you to register normal file associations for your proprietary formats, plus a default procedure to be used for arbitrary file types. When a user attempts to open one of your proprietary files, the file is opened as specified by the file association. If a user attempts to open an arbitrary file type, the file is opened as specified by the default procedure. The ability to define a default file opening procedure also supports two new features that are helpful to users:
If the user uses the Open With dialog box to associate a file name extension with an application, that choice is always respected. Applications that are subsequently installed are not permitted to take over the file association.
File associations can roam with the user. For example, assume that the user has associated MyProgram with .htm files on his or her primary computer. When that user attempts to open an .htm file on a different computer, MyProgram is used, if available.
The procedure for registering an application to accept arbitrary file classes is similar to that used to define shortcut menus for a file class. For a detailed discussion, see Extending Shortcut Menus.
Excluding an Application from the Open With Dialog Box
The applications that are listed in the Open With dialog box are registered as subkeys of HKEY_CLASSES_ROOT\Applications . However, many applications should not be used to open files that are not members of their associated file class. To exclude an application from the Open With dialog box, add a NoOpenWith REG_SZ value name to the application's subkey. For example, the following sample registry entry excludes MyProgram.exe from the Open With dialog box.
HKEY_CLASSES_ROOT
Applications
MyProgram.exe
NoOpenWith
It is also possible to exclude an application from the Open With dialog box by appending the application's file name to the Windows Explorer kill list. This option is not recommended and is discussed here only for completeness. The kill list is a REG_SZ value of the following key.
HKEY_LOCAL_MACHINE
Software
Microsoft
Windows
CurrentVersion
Explorer
FileAssociation
The kill list is a string consisting of the file names of the applications, separated by semicolons (

.
--------------------------------------------------------------------------------
© 2004 Microsoft Corporation. All rights reserved.
Partager