Search code examples
c#.neticons.net-assembly

Adding multiple Icons (Win32-Resource) to .NET-Application


it is possible to set the Application-Icon in the Project Properties. If you do this the exe will have this icon instead of the default one. this icon is a win32-resource and can also be accessed like this:

change icon

i want to have special icons for filetypes which are used by my program. to associate an filetype-icon i can specify it in the registry ("MyProg.exe,1" in the "DefaultIcon" Key).

so how to ADD MORE icons to the assembly that i can use it for the filetype-association?

thank you very much

ps: it is a WPF-Application (.NET 4.0)


Solution

  • Windows doesn't know anything about managed resources, you need to add unmanaged resources to your executable. In parapura's screenshot, you need to select the Resource file radio button. That requires a .res file, a binary file that's created by running the Windows SDK rc.exe tool on a .rc file. The .rc file is a simple text file that contains resource statements, similar to this:

    1 ICON "mainicon.ico"
    2 ICON "alternative1.ico"
    3 ICON "alternative2.ico"
    1 24 "app.manifest"
    

    Be sure to save this file into your project folder without utf-8 encoding, using Notepad is best. Create the required app.manifest file with Project + Add New Item, Application Manifest File. Add this .rc file to your project with Project + Add Existing Item. Double-click it and verify that you can see the icons and the manifest. Right-click the top node, Add Resource and click Version + New. Edit the version info, beware that it will no longer automatically match the attributes in AssemblyInfo.cs

    You can compile the .rc file into a .res file with the Visual Studio Command prompt:

    rc /r something.rc
    

    Which produces the .res file you can use in the project property tab. Doing this is a pre-build event is advisable but a bit hard to get right. The number of ways this can go wrong are numerous, good luck.