关联文件类型和图标
好的,我已经查看了所有内容,但我很难弄清楚如何将自定义文件类型与我的 C# 应用程序关联起来。我使用的是 Visual Studio 2008,由于公司部署的原因,我只能使用 .NET 2.0。
基本上我真正想做的就是能够允许用户双击我的自定义文件类型或将其拖放到应用程序图标上并加载数据,以及在文件本身上显示自定义图标。我可以通过对话框打开该文件,一切都很好。
尝试阅读 Microsoft 的在线文档很难理解,并且并没有真正提供我需要的所有详细信息。如果有人知道我在哪里可以找到好的教程或者可以解释它,我将不胜感激。
谢谢 帕特里克
Ok, I have looked all over and I am having a difficult time trying to figure out how to associate a custom file type with my C# application. I am using Visual Studio 2008 and am limited to .NET 2.0 due to company deployment.
Basically all I really want to do is be able to allow the user to double click my custom file type or drag and drop it on the app icon and it load the data, as well as display a custom icon on the file itself. I can open the file via a dialog box and everything works great.
Trying to read Micosoft's online documents is hard to understand and does not really give me all the details that I need. If anyone out there know's where I can find a good tutorial or can explain it I would appreciate it.
Thank you
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我几年前在新闻组中写过。浏览了一下,我发现它不是我写过的最清晰的,但相当完整。对于它的价值,我将其重印如下:
首先,您需要在
HKEY_CLASSES_ROOT
下创建一个子项,该子项将保存启动应用程序的命令。您应该将此键命名为半描述性的名称。您的文件扩展名将映射到此键。例如,默认情况下,TXT 文件映射到名为txtfile
的键。使用此设置的好处是,如果您的应用程序可以处理多个扩展,则可以将它们全部映射到此键。例如,许多图像编辑应用程序创建一个名为“imagefile”的子项,并将 .bmp、.jpg、.gif 等映射到该项。我们将您的密钥称为“JoeBlowFile”。接下来,您需要将新的 JoeBlowFile 键的“默认”值设置为一个文本字符串,向用户描述他们拥有的文件类型。这就是 Windows 资源管理器中“类型”下显示的内容。同样,以 TXT 文件为例,这里的一个好的字符串是“Text File”或“Text Document”。 (默认情况下是后者。)您的字符串可能是“Joe Blow Data”。
现在,在新项下,您可以创建另一个子项,名为“DefaultIcon”。顾名思义,这设置了与此类文件一起使用的图标。您应该创建一个自定义图标,以图形方式表示您的程序处理的文档。您可以将此图标另存为应用程序目录中的 ICO 文件,但更好的是,您可以将其作为资源包含在 EXE 或 DLL 中。无论哪种方式,您都需要将子项的默认值设置为表示 ICO、EXE 或 DLL 的完整路径和文件名的字符串。如果文件中有多个图标(特别是如果您将其作为资源包含在 EXE 或 DLL 中),您还需要添加一个逗号,以及一个从零开始的正索引号,表示您要使用的图标想要使用,或负资源 ID,使用您在资源脚本中为图标分配的任何 ID 的负数。例如,您的可能是“C:\Program Files\JoeBlow\JoeBlow.exe, 2”。
无论您是否选择使用
DefaultIcon
键,您现在都需要在JoeBlowFile
键下创建一个名为“shell”的子键。在shell
键下,您将为您希望用户能够从上下文菜单(右键单击菜单)对您的文件类型执行的每个命令创建单独的键。这些项目称为“动词”。为了保持一致性,其中之一应该是“open”——该键如果存在,将成为默认键(即,当用户双击您类型的文件时,将执行打开命令)。相反,您可以将“shell”键的默认值设置为您想要默认执行的动词。您可以选择将每个动词键的默认值设置为您希望在用户右键单击您类型的文件时出现在上下文菜单中的文本。在此文本中可以使用与号 (&) 来指定后面的字符将带下划线,这意味着用户可以按与该字符对应的键从上下文菜单中选择该命令。例如,对于“打开”键,您可以将“使用 &Joe Blow 的应用程序打开”作为默认值。然后,带有下划线 J 的文本将出现在该类型文件的上下文菜单中,用户可以按字母 J 启动 Joe Blow 的应用程序。不过,使用“打开”(及后续)键,您必须做的唯一一件事就是创建
另一个键作为该键的子键,称为“command”。命令键的默认值必须设置为代表执行该操作所需的命令的字符串。例如,“open”键下的命令键中的默认字符串可能是“C:\Program Files\JoeBlow\JoeBlow.exe”“%1”。请注意应用程序的路径\文件名和
%1
周围的引号。仅当路径或文件名中存在空格时,它们才需要在应用的路径\文件名周围,但对于 32 位应用,它们绝对是%1
周围的要求。%1
就像旧 MS-DOS 批处理 (.bat) 文件中的%1
一样。%1
被命令行上的第一个参数替换,在这种情况下,它成为您的应用程序应该打开的文件的文件名。因为您事先不知道路径或文件名是否包含您应该使用的文件open 将包含空格,您必须在
%1
两边加上引号。还应包括您的应用程序所需的其他参数。例如,“print”键下的“command”键中的默认值可能是“C:\Program Files\JoeBlow\JoeBlow.exe”“%1”/print”或“”C:\Program文件\JoeBlow\JoeBlow.exe" /print "%1""。这取决于您希望如何在应用程序中处理命令行参数。
您终于完成了
JoeBlowFile
键。剩下的就比较简单了。您只需在 HKEY_CLASSES_ROOT 下创建(如果尚不存在)另一个子项,并将其命名为与文档类型的扩展名相同的名称。要使用 txtfile 示例,名称将为“.txt”(带点,但不带引号)。您的(Joe Blow 的)可能是“.jbf”(Joe Blow 文件)。现在必须将此键的默认值设置为您创建的第一个键的名称,在我们使用的示例中是“JoeBlowFile”。就是这样。您已在注册表中完成。请记住,您必须确保您的应用程序
以与您在“shell”键下设置的命令一致的方式处理命令行。当您的应用程序启动时,Windows 不会自动为您打开该文件...您必须这样做。
从图形上看,它看起来像这样:
如果您还不知道如何修改注册表,请在 MSDN 中查找所有功能
以“Reg”开头,包括RegOpenKeyEx、RegCreateKeyEx和RegSetValueEx。您还可以通过创建“.reg”文件并简单地使用 ShellExecuteEx() 调用“regedit.exe /s”来完成此操作。 (
/s
可以防止 Regedit 弹出一个消息框,询问“您确定要将 [name of file.reg] 中的信息添加到注册表吗?”)REG 的格式文件简单直接。下面是一个示例 REG 文件,用于添加上面的“JoeBlow”示例:确保包含“REGEDIT4”作为文件的第一行,否则它将无法工作。还
确保在最后一行按 Enter 键,否则该行将不会被读入。总之,以这种方式将程序添加到注册表并不像听起来那么方便,因为如果您的应用程序安装在除 C:\Program Files\JoeBlow 之外的任何位置。
要将本机可访问的资源添加到 .NET 程序集中,您将需要一个资源脚本。资源脚本是纯文本文件,就像代码文件一样。事实上,它是代码;由资源编译器 rc.exe 编译的声明性代码。示例资源脚本如下:
这样做的最大缺点是您必须手动将版本信息添加到资源脚本中(除非您想完全放弃版本信息)。在我的应用程序中,我添加了一个自定义构建步骤,该步骤运行我编写的程序,该程序直接在可执行文件中更新版本信息,这样我就不必手动更新资源脚本中的版本号,但该程序不适合公开发布,否则超出了本文的范围。
现在您需要调用资源编译器将此资源脚本构建为二进制资源文件。将此脚本保存为 JoeBlow.rc,然后启动 Visual Studio 命令提示符。它位于 Visual Studio 开始菜单文件夹中的“工具”下。如果您没有安装 Visual Studio,我相信您会获得 rc.exe 作为 SDK 的一部分。微软似乎还提供了最新版本
一旦出现 VS cmd 提示符(或者路径中有 rc.exe),只需键入:
rc JoeBlow.rc
。
就这么简单 鉴于我包含的图标存在,上述资源脚本应该可以正确编译。这将在同一目录中创建一个名为 JoeBlow.res 的新文件。现在,假设您正在使用 Visual Studio,您所要做的就是编辑项目属性以包含此资源文件。
这些说明适用于 Visual Studio 2005 或 2008。我不记得如何在旧版本中执行此操作,或者即使可以,我还没有尝试过 2010,但可能类似。在“解决方案资源管理器”中右键单击该项目,然后选择“属性”(或从主菜单栏的“项目”菜单中选择“属性”)。在“应用程序”选项卡(您应该看到的第一个选项卡)上,底部是“资源”组框。在这里,您有两个选项:“图标和清单”或“资源文件”。选择后一个选项。这将启用文本框,您可以在其中键入(或浏览到)新资源文件 JoeBlow.res。
最后,只需构建您的项目,您就可以在浏览与应用程序关联的文件时以本机 PE 格式嵌入可访问 shell 的图标。现在,如果将
HKEY_CLASSES_ROOT\JoeBlowFile\DefaultIcon
的值设置为C:\Program Files\JoeBlow\JoeBlow.exe,1
(使用从零开始的索引号) ,或C:\Program Files\JoeBlow\JoeBlow.exe,-200
(使用资源标识符的负数,#define 上面为IDI_FILE
),您的图标将显示在资源管理器中查找 .jbf 文件。为了让新图标在安装后立即显示,您可能需要刷新 shell 的图标缓存。我在此处找到了有关如何执行此操作的说明。基本要点是将 shell 图标大小(位于
HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics
)从当前值更改为不同的值,然后再更改回来,广播WM_SETTINGCHANGE
每次更改后的消息。祝你好运。如果您还需要什么,请告诉我。
I wrote this up in the newsgroups a few years ago. Skimming through it, I find that it's not the clearest I've ever written, but it's fairly complete. For what it's worth, I've reprinted it below:
First you need to create a subkey under
HKEY_CLASSES_ROOT
that will hold the commands that start your application. You should name this key something semi-descriptive. Your file extension[s] will be mapped to this key. For example, TXT files are mapped to a key namedtxtfile
, by default. The benefit of using this setup is that if you have multiple extensions that your app can handle, you can map them all to this key. For example, manyimage editting apps create a subkey called something like "imagefile", and map .bmp, .jpg, .gif, etc. to that key. We'll call your key "JoeBlowFile". Next, you need to set the "default" value for your new JoeBlowFile key to a text string describing to the user just what type of file they have. This is what shows up in Windows Explorer under "Type". Again, to use the TXT file example, a good string here would be "Text File" or "Text Document". (It is the latter by default.) Your string might be "Joe Blow Data".
Now, under your new key, you can create another subkey, called "DefaultIcon". This, as its name suggests, sets the icon that is used with files of this type. You should create a custom icon that pictorially represents documents handled by your program. You can save this icon as an ICO file in your app's directory, but even better, you can include it as a resource in either your EXE or DLL. Either way, you'll then need to set the subkey's default value to a string representing the full path and filename of the ICO, EXE, or DLL. If there is more than one icon in the file (particularly likely if you include it as a resource in your EXE or DLL), you'll also need to add a comma, and either a zero-based positive index number representing which icon you'd like to use, or a negative resource ID, using the negative of whatever ID you've assigned your icon in your resource script. So yours could be, for example "C:\Program Files\JoeBlow\JoeBlow.exe, 2".
Whether or not you choose to use the
DefaultIcon
key, you now need to create a subkey named "shell" under yourJoeBlowFile
key. Under theshell
key, you'll create individual keys for each of the commands you'd like the user to be able to perform on your file type from the context menu (right-click menu). These items are called "verbs". For consistency, one of them should be "open"—this key, if it exists, will be the default (i.e. when a user double clicks on a file of your type, the open command will be performed). Instead, you can set the default value for the "shell" key equal to the verb you'd like to perform by default. You can optionally set the default value for each verb key to the text that you would like to appear in the context menu when a user right-clicks on a file of your type. An ampersand (&) can be used within this text to designate that the following character will be underlined, which means that the user can press the key corresponding to that character to select that command from the context menu. For instance, for the "open" key, you could put "Open with &Joe Blow's app" as the default value. That text then, with an underlined J, will appear in the context menu for files of that type, and a user can press the letter J to start Joe Blow's app.The only thing you have to do, though, with the "open" (and subsequent) keys, is create
another key as a subkey of that one called "command". The default value for the command key must be set to a string representing just that—the command required to perform that action. For instance, the default string in the command key under the "open" key might be ""C:\Program Files\JoeBlow\JoeBlow.exe" "%1"". Note the quotation marks around the path\filename for your app and around the
%1
. They are only neccessary around the path\filename of your app if there are any spaces in the path or filename, but they are absolutely a requirement around the%1
for 32-bit apps. The%1
is just like%1
in old MS-DOS batch (.bat) files.%1
is replaced with the first parameter on the command line, which in this case becomes the file name of the file your app is supposed to open. Because you do not know in advance if the path or filename containing the file you're supposed toopen will contain spaces, you must put the quotes around
%1
.Other required parameters for your app should also be included. For instance, the default value in the "command" key, under the "print" key might be ""C:\Program Files\JoeBlow\JoeBlow.exe" "%1" /print", or ""C:\Program Files\JoeBlow\JoeBlow.exe" /print "%1"". It's up to you how you want to process command line parameters in your app.
You're finally done with the
JoeBlowFile
key. The rest is relatively simple. You simply need to create (if it doesn't already exist) another subkey under HKEY_CLASSES_ROOT, and name it the same as the extension of your document type. To use the txtfile example, the name would be ".txt" (with the dot, but without the quotes). Yours (Joe Blow's) might be ".jbf" (for Joe Blow File). The default value for this key must now be set to the name of the first key your created, which in the example we've using is "JoeBlowFile".That's it. You're done in the registry. Do remember that you'll have to make sure your app
proccesses the command line in a manner consistent with the commands you set under the "shell" key. Window's won't open that file for you automatically when your app starts... you have to do it.
Graphically, it looks like this:
If you don't already know how to modify the registry, look in MSDN for all the functions
beginning with "Reg", including RegOpenKeyEx, RegCreateKeyEx, and RegSetValueEx. You can also do it the wimpy way by creating a ".reg" file and simply use ShellExecuteEx() to call "regedit.exe /s" on it. (The
/s
keeps Regedit from popping up a message box asking "Are you sure you want to add the information in [name of file.reg] to the registry?") The format of the a REG file is simple and straight forward. Here is an example REG file to add the "JoeBlow" example from above:Make sure that you include "REGEDIT4" as the first line of the file, or it won't work. Also
make sure to press enter on the last line, or that line won't be read in. Altogether, adding your program to the registry this way is not as convenient as it sounds, because you'll have to modify your REG file if your app is installed anywhere except to C:\Program Files\JoeBlow.
To add native-accessible resources to a .NET assembly, you will need a resource script. A resource script is a plain text file, like a code file. In fact, it is code; declarative code that is compiled by the resource compiler, rc.exe. A sample resource script follows:
The biggest drawback to doing this is that you have to add the version information manually to your resource script (unless you want to just forgo the version information altogether). In my applications, I add a custom build step that runs a program I wrote that updates the version information directly in the executable so that I don't have to manually update the version number in the resource script, but that program is not suitable for public release and is otherwise beyond the scope of this post.
Now you need to invoke the resource compiler to build this resource script into a binary resource file. Save this script as JoeBlow.rc, then start a Visual Studio Command Prompt. It's under Tools in the Visual Studio start menu folder. If you don't have Visual Studio installed, I believe you get rc.exe as part of the SDK. Microsoft also seems to be offering the latest version here.
Once at a VS cmd prompt (or otherwise have rc.exe in your path), just type:
rc JoeBlow.rc
Simple as that. The above resource script should compile without errors, given that the icons I've include exist. This will create a new file in the same directory called JoeBlow.res. Now, assuming you are using Visual Studio, all you have to do is edit the project properties to include this resource file.
These directions are for Visual Studio 2005 or 2008. I don't remember how to do this, or even if you can, in older versions, and I haven't tried out 2010, yet, but it's probably similar. Right-click on the project in Solution Explorer and select Properties (or select Properties from the Project menu on the main menu bar). On the Application tab, which is the first tab you should see, at the bottom is a Resources group box. Here, you have two options: "Icon and manifest", or "Resource File". Select the latter option. This will enable the text box where you can type (or browse to) your new resource file, JoeBlow.res.
Lastly, just build your project, and presto, you have embedded icons in native PE format accessible to the shell when browsing files associated to your app. Now if you set the value of
HKEY_CLASSES_ROOT\JoeBlowFile\DefaultIcon
to eitherC:\Program Files\JoeBlow\JoeBlow.exe,1
(using the zero-based index number), orC:\Program Files\JoeBlow\JoeBlow.exe,-200
(using the negative of the resource identifier, #defined above asIDI_FILE
), your icon will show up in Explorer for .jbf files.To get your new icons to show up immediately upon installation, you may need to refresh the shell's icon cache. I've found instructions on how to do that here. The basic gist is to change the shell icon size (at
HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics
) from its current value to a different one and back again, broadcasting aWM_SETTINGCHANGE
message after each change.Good luck. Let me know if you need anything else.
这篇 CodeProject 文章 有一些源代码,演示了通过代码进行文件关联。
This CodeProject article has some source code demonstrating file association by code.
这里讨论了如何使用命令行实用程序
assoc
和 < code>ftype 来执行此操作。您可以在程序部署期间(或应用程序运行时)调用命令 shell 来执行此操作。Here is a discussion about how to use command-line utilities
assoc
andftype
to do this. You could invoke the command shell during a program deployment (or when the application is run) to do this.