"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Associate Custom Executables with File Extensions in C#?

How to Associate Custom Executables with File Extensions in C#?

Posted on 2025-02-18
Browse:158

How to Associate Custom Executables with File Extensions in C#?

Associate custom executable with file type in C#

]

Associate a specific file extension with a custom executable in C#, allowing the user to start the executable with the associated file as a parameter when clicking on the file in File Explorer. In addition, you can also specify the desired icon for the file extension.

Solutions

While .NET does not provide an API to directly manage file associations, you can use the registry class to manipulate necessary key values.

  1. Create registry key: Create a registry key with a file extension as its name under HKEY_CLASSES_ROOT (for example, ".txt"). Set its default value to a unique name of the file type (for example, "Acme.TextFile").
  2. Create another registry key: Create a registry key under HKEY_CLASSES_ROOT with the name of the unique file type name in step 1 (for example, "Acme.TextFile").
  3. Add child: Named "DefaultIcon", its default value is the path of the desired icon file.
  4. Add simultaneous item : Named "shell", used to save context menu operations. For each operation, create a child whose default value is set to the path of the executable, followed by a space and "%1" (file path placeholder).

Example of registry file associated with .txt with EmEditor:

]
[HKEY_CLASSES_ROOT\.txt]
@="emeditor.txt"

[HKEY_CLASSES_ROOT\emeditor.txt]
@="Text Document"

[HKEY_CLASSES_ROOT\emeditor.txt\DefaultIcon]
@="%SystemRoot%\\SysWow64\\imageres.dll,-102"

[HKEY_CLASSES_ROOT\emeditor.txt\shell]

[HKEY_CLASSES_ROOT\emeditor.txt\shell\open]

[HKEY_CLASSES_ROOT\emeditor.txt\shell\open\command]
@="\"C:\\Program Files\\EmEditor\\EMEDITOR.EXE\" \"%1\""

[HKEY_CLASSES_ROOT\emeditor.txt\shell\print]

[HKEY_CLASSES_ROOT\emeditor.txt\shell\print\command]
@="\"C:\\Program Files\\EmEditor\\EMEDITOR.EXE\" /p \"%1\""
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3