如何将文件扩展名关联到 C# 中的当前可执行文件 [英] How to associate a file extension to the current executable in C#

查看:26
本文介绍了如何将文件扩展名关联到 C# 中的当前可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件扩展名与 C# 中的当前可执行文件相关联.这样,当用户之后在资源管理器中单击该文件时,它将以给定的文件作为第一个参数运行我的可执行文件.理想情况下,它还会将给定文件扩展名的图标设置为我的可执行文件的图标.谢谢大家.

解决方案

似乎没有用于直接管理文件关联的 .Net API,但您可以使用 Registry 类来读取和写入您需要的键.

您需要在 HKEY_CLASSES_ROOT 下创建一个密钥,并将名称设置为您的文件扩展名(例如:.txt").将此键的默认值设置为文件类型的唯一名称,例如Acme.TextFile".然后在 HKEY_CLASSES_ROOT 下创建另一个键,名称设置为Acme.TextFile".添加一个名为DefaultIcon"的子键,并将键的默认值设置为包含您希望用于此文件类型的图标的文件.添加另一个名为shell"的兄弟.在shell"键下,为您希望通过资源管理器上下文菜单提供的每个操作添加一个键,将每个键的默认值设置为可执行文件的路径,后跟一个空格和%1"来表示路径到所选文件.

例如,这里有一个示例注册表文件,用于在 .txt 文件和 EmEditor 之间创建关联:

<前>Windows 注册表编辑器 5.00 版[HKEY_CLASSES_ROOT.txt]@="编辑器.txt"[HKEY_CLASSES_ROOTemeditor.txt]@="文本文档"[HKEY_CLASSES_ROOTemeditor.txtDefaultIcon]@="%SystemRoot%\SysWow64\imageres.dll,-102"[HKEY_CLASSES_ROOTemeditor.txtshell][HKEY_CLASSES_ROOTemeditor.txtshellopen][HKEY_CLASSES_ROOTemeditor.txtshellopencommand]@=""C:\Program Files\EmEditor\EMEDITOR.EXE" "%1""[HKEY_CLASSES_ROOTemeditor.txtshellprint][HKEY_CLASSES_ROOTemeditor.txtshellprintcommand]@=""C:\Program Files\EmEditor\EMEDITOR.EXE"/p "%1""

I'd like to to associate a file extension to the current executable in C#. This way when the user clicks on the file afterwards in explorer, it'll run my executable with the given file as the first argument. Ideally it'd also set the icon for the given file extensions to the icon for my executable. Thanks all.

解决方案

There doesn't appear to be a .Net API for directly managing file associations but you can use the Registry classes for reading and writing the keys you need to.

You'll need to create a key under HKEY_CLASSES_ROOT with the name set to your file extension (eg: ".txt"). Set the default value of this key to a unique name for your file type, such as "Acme.TextFile". Then create another key under HKEY_CLASSES_ROOT with the name set to "Acme.TextFile". Add a subkey called "DefaultIcon" and set the default value of the key to the file containing the icon you wish to use for this file type. Add another sibling called "shell". Under the "shell" key, add a key for each action you wish to have available via the Explorer context menu, setting the default value for each key to the path to your executable followed by a space and "%1" to represent the path to the file selected.

For instance, here's a sample registry file to create an association between .txt files and EmEditor:

Windows Registry Editor Version 5.00

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

[HKEY_CLASSES_ROOTemeditor.txt]
@="Text Document"

[HKEY_CLASSES_ROOTemeditor.txtDefaultIcon]
@="%SystemRoot%\SysWow64\imageres.dll,-102"

[HKEY_CLASSES_ROOTemeditor.txtshell]

[HKEY_CLASSES_ROOTemeditor.txtshellopen]

[HKEY_CLASSES_ROOTemeditor.txtshellopencommand]
@=""C:\Program Files\EmEditor\EMEDITOR.EXE" "%1""

[HKEY_CLASSES_ROOTemeditor.txtshellprint]

[HKEY_CLASSES_ROOTemeditor.txtshellprintcommand]
@=""C:\Program Files\EmEditor\EMEDITOR.EXE" /p "%1""

这篇关于如何将文件扩展名关联到 C# 中的当前可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆