Home > Backend Development > C++ > How to Associate a File Extension with Your C# Executable?

How to Associate a File Extension with Your C# Executable?

DDD
Release: 2025-01-20 16:19:09
Original
999 people have browsed it

How to Associate a File Extension with Your C# Executable?

Associating a file extension with an executable file in C#

After associating a file extension with the current executable, users can open the file in Explorer and automatically run your program using the file as an argument. This article will guide you on how to do this in C# by manipulating registry keys.

Solution:

While C# lacks a dedicated API for managing file associations, you can take advantage of registry manipulation capabilities. This process involves modifying a specific key in the registry:

  1. HKEY_CLASSES_ROOT subkey: Create a key under HKEY_CLASSES_ROOT with its name set to the desired file extension (e.g. .txt). Assign this key's default value to a unique name that represents the file type, such as Acme.TextFile.
  2. Default icon key: Under HKEY_CLASSES_ROOT, create another key with a name assigned to a unique file type name (e.g. Acme.TextFile). Add a subkey called DefaultIcon and set its default value to the file path of the icon for the desired file type.
  3. Context Menu Actions: To allow actions to appear in the Explorer context menu, create a subkey named shell under the unique file type key (Acme.TextFile). Add subkeys for each action and set the default value for each subkey to the path to the executable file, followed by a space and %1 (the file path placeholder).

Example:

The following registry file associates the .txt file with EmEditor, sets the icon and defines open and print operations:

<code>[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\""</code>
Copy after login

By implementing these registry modifications, you can successfully associate file extensions with executable files, providing a seamless user experience for opening and interactively using the file.

The above is the detailed content of How to Associate a File Extension with Your C# Executable?. 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