关联文件类型和图标

发布于 2024-08-14 23:12:01 字数 299 浏览 6 评论 0原文

好的,我已经查看了所有内容,但我很难弄清楚如何将自定义文件类型与我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪菊怪哟 2024-08-21 23:12:01

几年前在新闻组中写过。浏览了一下,我发现它不是我写过的最清晰的,但相当完整。对于它的价值,我将其重印如下:


首先,您需要在 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”。

针对上一段内容的 C# 开发人员注意事项。不幸的是,C# 项目不能有资源脚本。通过将资源添加到项目并指定它们为“嵌入式资源”来添加到 .NET 应用程序的资源包含在新的 .NET 特定格式中,该格式与以前的方法不兼容。在 VS.NET 上使用 C# 可以正确嵌入到应用程序中的唯一图标是应用程序图标,可从项目属性访问。如果您需要其他图标,例如
图标来表示由您的应用程序处理的文档文件,而不是表示应用程序本身 - 您需要包含 ICO 文件本身,使用嵌入图标的 C++ 编译 DLL,或者创建并编译资源脚本并将其包含在内从项目属性中添加到您的项目中。

无论您是否选择使用 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""。这取决于您希望如何在应用程序中处理命令行参数。

关于上面提到的“%1”等可替换参数的注释。显然,“%1”参数/可以/被替换为要打开的文件名。情况并非总是如此,而且我还没有弄清楚 Windows 使用什么标准来确定它将通过(短还是长)。可能是,如果注册表中列出的可执行路径是长文件名,Windows 会将 %1 替换为长文件名来启动,但如果可执行路径是短文件名,或者可以解释为 1,Windows 将替换 < code>%1 带有短文件名。如果您想确保始终获得文件名,请使用“%L”。您可以使用大写的 L(就像我所做的那样)或小写的 L,但我更喜欢使用大写,因为小写的“l”看起来太像数字“1”。

此外,如果您的程序知道如何处理 Shell Item ID,您可以使用“%i”参数获取那个,而不是长文件名。同样,大写或小写“i”同样合适,但我发现大写“I”很难与小写“l”和数字“1”区分开。如果您不知道 Shell Item ID 是什么,也没关系。您可能永远不需要使用它。

您终于完成了 JoeBlowFile 键。剩下的就比较简单了。您只需在 HKEY_CLASSES_ROOT 下创建(如果尚不存在)另一个子项,并将其命名为与文档类型的扩展名相同的名称。要使用 txtfile 示例,名称将为“.txt”(带点,但不带引号)。您的(Joe Blow 的)可能是“.jbf”(Joe Blow 文件)。现在必须将此键的默认值设置为您创建的第一个键的名称,在我们使用的示例中是“JoeBlowFile”。

就是这样。您已在注册表中完成。请记住,您必须确保您的应用程序
以与您在“shell”键下设置的命令一致的方式处理命令行。当您的应用程序启动时,Windows 不会自动为您打开该文件...您必须这样做。

从图形上看,它看起来像这样:

HKEY_CLASSES_ROOT 
| +--.jbf = JoeBlowFile 
| 
+--JoeBlowFile = Joe Blow Data 
     | 
     +--DefaultIcon = C:\Program Files\JoeBlow\JoeBlow.exe, 2 
     | 
     +--Shell 
          | 
          +--open = Open with &Joe Blow's App 
          |     | 
          |     +--command = "C:\Program Files\JoeBlow\JoeBlow.exe" "%1" 
          | 
          +--print 
               | 
               +--command = "C:\Program Files\JoeBlow\JoeBlow.exe" "%1" /print

如果您还不知道如何修改注册表,请在 MSDN 中查找所有功能
以“Reg”开头,包括RegOpenKeyEx、RegCreateKeyEx和RegSetValueEx。您还可以通过创建“.reg”文件并简单地使用 ShellExecuteEx() 调用“regedit.exe /s”来完成此操作。 (/s 可以防止 Regedit 弹出一个消息框,询问“您确定要将 [name of file.reg] 中的信息添加到注册表吗?”)REG 的格式文件简单直接。下面是一个示例 REG 文件,用于添加上面的“JoeBlow”示例:

REGEDIT4 

[HKEY_CLASSES_ROOT\.jbf]
@="JoeBlowFile"

[HKEY_CLASSES_ROOT\JoeBlowFile]
@="Joe Blow Data"

[HKEY_CLASSES_ROOT\JoeBlowFile\DefaultIcon]
@="C:\\Program Files\\JoeBlow\\JoeBlow.exe, 2"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell]

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\open]
@="Open with &Joe Blow's app"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\open\command]
@="\"C:\\Program Files\\JoeBlow\\JoeBlow.exe\" \"%1\""

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\print]
@="&Print"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\print\command]
@="\"C:\\Program Files\\JoeBlow\\JoeBlow.exe \"%1\" /print" 

确保包含“REGEDIT4”作为文件的第一行,否则它将无法工作。还
确保在最后一行按 Enter 键,否则该行将不会被读入。总之,以这种方式将程序添加到注册表并不像听起来那么方便,因为如果您的应用程序安装在除 C:\Program Files\JoeBlow 之外的任何位置。

以上说明针对的是用户使用 C 或 C 直接编程 Win32 API
C++。对于 .NET 上的 C# 来说,这要容易一些。查看Registry类,或者您甚至可以使用VS.NET中的部署项目以图形方式完成大部分工作。


要将本机可访问的资源添加到 .NET 程序集中,您将需要一个资源脚本。资源脚本是纯文本文件,就像代码文件一样。事实上,它是代码;由资源编译器 rc.exe 编译的声明性代码。示例资源脚本如下:

#include <windows.h>

#define IDI_APP    100
#define IDI_FILE   200
#define ID_VERSION   1

IDI_APP  ICON "Resources\\Application.ico"
IDI_FILE ICON "Resources\\JowBlowFile.ico"

ID_VERSION VERSIONINFO
    FILEVERSION 1, 0, 19, 186     // change this to your version
    PRODUCTVERSION 1, 0, 19, 186  // change this to your version
    FILEOS VOS__WINDOWS32
    FILETYPE VFT_APP {
        BLOCK "StringFileInfo" {
            BLOCK "040904B0" { // 0x409 = U.S. English, 0x04B0 = dec 1200 = Unicode
                VALUE "CompanyName",      "Joe Blow, Inc.\0"
                VALUE "FileDescription",  "Joe Blow's App\0"
                VALUE "FileVersion",      "1.0.19.186\0" // change this to your version
                VALUE "InternalName",     "JoeBlow\0"
                VALUE "LegalCopyright",   "Copyright © 2008-2009 Joe Blow Incorporated\0"
                VALUE "OriginalFilename", "JoeBlow.exe\0"
                VALUE "ProductName",      "Joe Blow\0"
                VALUE "ProductVersion",   "1.0.19.189\0" // change this to your version
            }
        }
        BLOCK "VarFileInfo" {
            VALUE "Translation", 0x409 /*U.S. English*/, 1200 /*Unicode*/
        }
    }

这样做的最大缺点是您必须手动将版本信息添加到资源脚本中(除非您想完全放弃版本信息)。在我的应用程序中,我添加了一个自定义构建步骤,该步骤运行我编写的程序,该程序直接在可执行文件中更新版本信息,这样我就不必手动更新资源脚本中的版本号,但该程序不适合公开发布,否则超出了本文的范围。

现在您需要调用资源编译器将此资源脚本构建为二进制资源文件。将此脚本保存为 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 named txtfile, 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, many
image 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".

A note for C# developers on the above paragraph. Unfortunately, C# projects can't have resource scripts. Resources added to .NET applications by adding them to the project and designating them an "Embedded Resource" are included in a new .NET-specific format that's not compatible with previous methods. The only icon you can correctly embed in your application using C# on VS.NET is the application icon, accessible from the project properties. If you need additional icons—e.g. an
icon to represent a document file handled by your app rather than to represent the app itself—you'll need to either include the ICO files themselves, compile a DLL with C++ with your icons embedded, or create and compile a resource script and include it in your project from the project properties.

Whether or not you choose to use the DefaultIcon key, you now need to create a subkey named "shell" under your JoeBlowFile key. Under the shell 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 to
open 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.

A note on replaceable parameters like "%1", mentioned above. Apparently, the "%1" parameter /may/ be replaced with the short filename to be opened. This isn't always the case, and I haven't figured out what criteria Windows uses to determine which it will pass—short or long. It may be that if the executable path listed in the registry is a long filename, Windows will replace %1 with the long filename to start, but if the executable path is a short filename, or can be interpreted as one, Windows will replace %1 with the short filename. If you want to be sure that you always get the long filename, use "%L" instead. You can use an uppercase L (as I've done) or a lowercase one, but I prefer to use uppercase because lowercase "l" looks way too much like the number "1".

What's more, if your program knows how to deal with Shell Item IDs, you can get that instead of the long filename with the "%i" parameter. Again, upper- or lowercase "i" are equally suitable, but I find uppercase "I" harder to distiguish from lowercase "l" and the number "1". If you don't know what a Shell Item ID is, it's okay. You'll probably never need to use one.

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:

HKEY_CLASSES_ROOT 
| +--.jbf = JoeBlowFile 
| 
+--JoeBlowFile = Joe Blow Data 
     | 
     +--DefaultIcon = C:\Program Files\JoeBlow\JoeBlow.exe, 2 
     | 
     +--Shell 
          | 
          +--open = Open with &Joe Blow's App 
          |     | 
          |     +--command = "C:\Program Files\JoeBlow\JoeBlow.exe" "%1" 
          | 
          +--print 
               | 
               +--command = "C:\Program Files\JoeBlow\JoeBlow.exe" "%1" /print

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:

REGEDIT4 

[HKEY_CLASSES_ROOT\.jbf]
@="JoeBlowFile"

[HKEY_CLASSES_ROOT\JoeBlowFile]
@="Joe Blow Data"

[HKEY_CLASSES_ROOT\JoeBlowFile\DefaultIcon]
@="C:\\Program Files\\JoeBlow\\JoeBlow.exe, 2"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell]

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\open]
@="Open with &Joe Blow's app"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\open\command]
@="\"C:\\Program Files\\JoeBlow\\JoeBlow.exe\" \"%1\""

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\print]
@="&Print"

[HKEY_CLASSES_ROOT\JoeBlowFile\Shell\print\command]
@="\"C:\\Program Files\\JoeBlow\\JoeBlow.exe \"%1\" /print" 

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.

The above instructions were aimed at a user programming directly to the Win32 API using C or
C++. For C# on .NET, it's rather a bit easier. See the Registry class, or you can even do much of it graphically using a deployment project in VS.NET.


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:

#include <windows.h>

#define IDI_APP    100
#define IDI_FILE   200
#define ID_VERSION   1

IDI_APP  ICON "Resources\\Application.ico"
IDI_FILE ICON "Resources\\JowBlowFile.ico"

ID_VERSION VERSIONINFO
    FILEVERSION 1, 0, 19, 186     // change this to your version
    PRODUCTVERSION 1, 0, 19, 186  // change this to your version
    FILEOS VOS__WINDOWS32
    FILETYPE VFT_APP {
        BLOCK "StringFileInfo" {
            BLOCK "040904B0" { // 0x409 = U.S. English, 0x04B0 = dec 1200 = Unicode
                VALUE "CompanyName",      "Joe Blow, Inc.\0"
                VALUE "FileDescription",  "Joe Blow's App\0"
                VALUE "FileVersion",      "1.0.19.186\0" // change this to your version
                VALUE "InternalName",     "JoeBlow\0"
                VALUE "LegalCopyright",   "Copyright © 2008-2009 Joe Blow Incorporated\0"
                VALUE "OriginalFilename", "JoeBlow.exe\0"
                VALUE "ProductName",      "Joe Blow\0"
                VALUE "ProductVersion",   "1.0.19.189\0" // change this to your version
            }
        }
        BLOCK "VarFileInfo" {
            VALUE "Translation", 0x409 /*U.S. English*/, 1200 /*Unicode*/
        }
    }

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 either C:\Program Files\JoeBlow\JoeBlow.exe,1 (using the zero-based index number), or C:\Program Files\JoeBlow\JoeBlow.exe,-200 (using the negative of the resource identifier, #defined above as IDI_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 a WM_SETTINGCHANGE message after each change.

Good luck. Let me know if you need anything else.

梦回梦里 2024-08-21 23:12:01

这篇 CodeProject 文章 有一些源代码,演示了通过代码进行文件关联。

This CodeProject article has some source code demonstrating file association by code.

谁与争疯 2024-08-21 23:12:01

这里讨论了如何使用命令行实用程序 assoc 和 < code>ftype 来执行此操作。您可以在程序部署期间(或应用程序运行时)调用命令 shell 来执行此操作。

Here is a discussion about how to use command-line utilities assoc and ftype to do this. You could invoke the command shell during a program deployment (or when the application is run) to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文