Create a Windows File Association

Source: Internet
Author: User

The file association function has been added to the Windows system for a long time, which greatly increases the convenience of daily use of the operating system. The so-called file association means that the system automatically associates a file with a specified extension to the corresponding application.ProgramSuch as. DOC files automatically associated with Microsoft Word by default. When you double-click the. DOC file, the system automatically opens it with Microsoft Word. This articleArticleThis section describes how to manually set file associations in Windows or automatically set file associations in a program.

How to manually set File Association

For Windows systems (including Windows XP, Windows Vista, and Windows 7), you can manually set file associations. The following uses Windows 7 as an example to explain how to set these settings.

    • Open Control Panel-> Program-> default program-> associated file type or protocol and application:

On the screenshot capture page, we can see that the. DOC file type is "Microsoft Word 97-2003 Document", and its default associated application is Microsoft Word. You can double-click any association record in the above list to set your own Association. For example, you can change the TXT file association from the default WordPad application to Microsoft Word. Double-click the TXT file association record. The following dialog box is displayed. Select the Microsoft Word application to open the TXT file by default.

How to add File Associations programmatically

In addition to the manual setting of file associations described above, we often need to set file associations automatically by the program. For example, a program has a default file format, and we often want to automatically associate the file format with the program. Currently, many installation packages (such as Install Shield) provide similar functions, which can help us set file associations during the installation process. For detailed settings, refer to the relevant software user manual. Here I want to introduce how to write a program to implement File Association (the classic use case of this technology is to first check the file association settings and add the specified file association settings when necessary when the program is running ).

  • Background
  • in windows, the Registry is used to save all file association settings of the current system. They generally exist in the following registries:

    • HKEY_LOCAL_MACHINE \ SOFTWARE \ Classes: This registry entry includes the default file association settings for all users, as shown in:

    • HKEY_CURRENT_USER \ Software \ Classes: This registry key contains the file association settings applicable only to the current user (it will overwrite the settings in the HKEY_LOCAL_MACHINE item ). As shown in:

    in addition to the two registry keys and file associations above, there is also a very important registry key: hkey_classes_root

    the settings in this registry project are the key to ensuring that the windows browser can select the right application to open the corresponding file. After Windows 2000, the file association settings in the registry project are included in the two registry items mentioned above. The hkey_class_root registry key becomes an image of the preceding two registry items (note that the settings under HKEY_CURRENT_USER will overwrite the settings under HKEY_LOCAL_MACHINE. To update File Association settings, you must update the registry key under "HKEY_CURRENT_USER \ Software \ classess" or "HKEY_LOCAL_MACHINE \ SOFTWARE \ classess" instead of directly updating the registry key under hkey_class_root.

  • Program instance

The typical program logic for setting file association is as follows:

    1. Check whether the current file type has been associated with an application (check the hkey_class_root registry key because it contains settings from HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER ). If the file type is not set, go to the next step. If the related file association has been set, your business logic determines that you do not need to overwrite the file association. If necessary, go to the next step.
    2. Add the corresponding file to the registry key (if you want this setting to be valid only for the current user, add it to HKEY_CURRENT_USER. If you want this setting to be valid for all users on the machine, add HKEY_LOCAL_MACHINE.
    3. Refresh the updated registry to make it take effect.

DetailsCodeAs follows:

Bool setfileassociation (const wstring & Ext,

Const wstring & applicationname,

Const wstring & progid,

Const wstring & defaulticon,

Const wstring & description)

{

Bool Bret = false;

Hkey hextkey;

// Check whether file association has been set for this file type

If (regopenkey (hkey_classes_root, ext. c_str (), & hextkey) = error_success)

{

Hkey;

Wstring hkcuprefix = _ T ("Software \ Classes \\");

// Set File Association (Note: You must set a set of values, including the file type Extension, description, progid, default icon, and Windows Shell startup Command Format)

Wstring fullext = hkcuprefix + ext;

If (error_success! = Regcreatekey (HKEY_CURRENT_USER, fullext. c_str (), & hkey)

| Error_success! = Regsetvalue (hkey, _ T (""), REG_SZ, progid. c_str (), progid. Length () * sizeof (wchar_t) + 1)

| Error_success! = Regclosekey (hkey ))

{

Throw exception (ext. c_str ());

}

Wstring fullprogid = hkcuprefix + progid;

If (error_success! = Regcreatekey (HKEY_CURRENT_USER, fullprogid. c_str (), & hkey)

| Error_success! = Regsetvalue (hkey, _ T (""), REG_SZ, description. c_str (), description. Length () * sizeof (wchar_t) + 1)

| Error_success! = Regclosekey (hkey ))

{

Throw exception (ext. c_str ());

}

Wstring striconkey = fullprogid + _ T (" \ Defaulticon ");

If (error_success! = Regcreatekey (HKEY_CURRENT_USER, striconkey. c_str (), & hkey)

| Error_success! = Regsetvalue (hkey, _ T (""), REG_SZ, defaulticon. c_str (), defaulticon. Length () * sizeof (wchar_t) + 1)

| Error_success! = Regclosekey (hkey ))

{

Throw exception (ext. c_str ());

}

Wstring strshellkey = fullprogid + _ T (" \ Shell ");

If (error_success! = Regcreatekey (HKEY_CURRENT_USER, strshellkey. c_str (), & hkey)

| Error_success! = Regsetvalue (hkey, _ T (""), REG_SZ, _ T ("open"), 4 * sizeof (wchar_t) + 1)

| Error_success! = Regclosekey (hkey ))

{

Throw exception (ext. c_str ());

}

Wstring strcommandkey = fullprogid + _ T (" \ Shell \ open \ command ");

Wstring strcommand = applicationname + _ T ("\" % 1 \"");

If (error_success! = Regcreatekey (HKEY_CURRENT_USER, strcommandkey. c_str (), & hkey)

| Error_success! = Regsetvalue (hkey, _ T (""), REG_SZ, strcommand. c_str (), strcommand. Length () * sizeof (wchar_t) + 1)

| Error_success! = Regclosekey (hkey ))

{

Throw exception (ext. c_str ());

}

// Notification system for the above settings to take effect

Shchangenovel (shcn_assocchanged, shcnf_idlist, null, null );

Bret = true;

}

Return Bret;

}


Del. icio. us: File Association, File Association

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.