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

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

问题描述

我要到一个文件扩展到C#中的当期可执行关联。
当用户点击该文件之后在资源管理器这样的话,它会用给定的文件作为第一个参数运行我的可执行文件。
理想的情况下,也最好设置给定文件扩展名的可执行我的图标上。
感谢所有。

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.

推荐答案

似乎没有成为直接管理文件关联一个.NET API,但你可以使用注册表类阅读和写作,你需要的按键。

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.

您需要创建设置为您的文件扩展名HKEY_CLASSES_ROOT下的一个键(如:名.txt)。设置此键为唯一的名称为您的文件类型,如Acme.TextFile的默认值。然后创建一个设置为Acme.TextFile的名义下HKEY_CLASSES_ROOT另一个关键。添加一个名为的DefaultIcon的子项,设置键值为包含要使用此文件类型的图标文件的默认值。添加一个名为壳的另一个兄弟。在壳键,增加对您希望通过资源管理器右键菜单中有可用的每个操作键,每个键的路径到你的可执行后跟一个空格和%1重新$ P设置的默认值$ psent路径选择的文件。

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.

例如,下面是一个示例注册表文件创建.txt文件和EmEditor中之间的关联:

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_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\""

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

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