Home > Backend Development > C++ > How to Programmatically Create File-Extension Associations in C ?

How to Programmatically Create File-Extension Associations in C ?

DDD
Release: 2024-11-28 04:45:12
Original
533 people have browsed it

How to Programmatically Create File-Extension Associations in C  ?

Creating File-Extension Associations in C

When a user double-clicks a file associated with a specific application, the operating system launches that application and passes the filename as an argument. This association is established through registry entries.

Registry Configuration

To register a file extension with an application in C , you need to create two registry keys:

  1. ProgID: Contains properties such as icon, description, and applications used to open the file.
  2. File Extension: Associates the extension with the ProgID, mapping the extension to its file type.

Steps:

  1. Create ProgID:

    HKEY_CURRENT_USER\Software\Classes\<ProgID>
    Copy after login
  2. Associate Extension with ProgID:

    HKEY_CURRENT_USER\Software\Classes\.<Extension>
    Copy after login

    Set the value to the ProgID.

Example:

To associate the .blerg extension with the blergcorp.blergapp.v1 file type, create the following registry keys:

HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command
@="c:\path\to\app.exe \"%1\""
HKEY_CURRENT_USER\Software\Classes\.blerg
@"blergcorp.blergapp.v1"
Copy after login

Programmatic Approach:

Use the SetValue function in C to set these registry values. If the keys or values don't exist, they will be automatically created.

Considerations:

  • Hive Selection: Set keys in HKEY_CURRENT_USERSoftwareClasses for per-user associations, or in HKEY_LOCAL_MACHINESoftwareClasses for system-wide associations.
  • Removal of Associations: When an application is uninstalled, its registry entries should be removed to avoid leaving unused entries.

Conclusion:

By creating the appropriate registry entries, you can associate file extensions with your C application, enabling users to open associated files directly through your application.

The above is the detailed content of How to Programmatically Create File-Extension Associations in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template