Embed multiple icons in WPF EXE

2019-02-05 08:27发布

问题:

I have a WPF assembly in which I would like to embed five icons for different filetypes associated with my application. How can I embed these icons into my EXE?

@smoore @Groky @ChrisF, thank you. Unfortunately, this is not what I asked. I see that my question was quite vague 0_­°. Let me rephrase the question:


I have icons, say Application.ico, Document.ico, etc. as resources in my WPF projects. I access these icons in most cases with the following:

<Image Source="/MyAssembly;component/Resources/Icons/Application.ico" />

That works every single time. I know that.


What my question is about is how can I use the same icons from Windows Explorer for file associations in the registry. I want to be able to access the icons with a path like:

C:\Program Files\MyApp\MyApp.exe,1

Like in how icons are associated with filetypes in HKEY_CLASSES_ROOT.

AFAIK, I should use a resource file (.rc), compile and merge it with my EXE. Something along the lines of:

101 RT_ICON Application.ico
102 RT_ICON Document.ico
// etc...

Is this the right way in WPF? Is there an alternative, especially since this method seems to lead to the erasure of the assembly version from my AssemblyInfo.cs. I am still prepared to have to write the versioning info in the resource instead of the assembly's info.

回答1:

I have found one solution! It's not perfect but it does what I want! As I used very long Scandinavian nights to find the solution, I feel that I have to share it here.

Here's what I did:

1) Wrote a dumb console C# app.

class ResTest {
    static void Main() {
        System.Console.WriteLine("Hello World!");
    }
}

2) Did a simple csc restest.cs to test that my code worked.

3) Opened Notepad and wrote the following in a file I dubbed App.rc.

101 ICON "Application.ico"
102 ICON "Document.ico"
103 ICON "Help.ico"

4) Ran rc /v App.rc, the Resource Compiler. A new file, App.res had appeared.

5) Reran csc but this time:

csc /win32res:App.res restest.cs

6) restest.exe had now the icon with the ID of 101 and I could find the two other icons in Axialis IconWorkshop.


Now, I noticed that my assembly information (version, product name, blah blah blah) had disappeared. I googled VS_VERSION_INFO and came about MSDN's article about the VERSIONINFO structure which in RC files defines the attributes I need.

I would have preferred a more 'automated' method, but I must do what I can using the C# Express and the Windows Vista SDK.

-- Hope that you can use this...



回答2:

  1. Create App.rc:

    101 ICON "Application.ico"
    102 ICON "Document.ico"
    103 ICON "Help.ico"
    
  2. Compile:

    rc App.rc
    
  3. Add App.res to your project (leave None as your Build Action).
  4. Build.

All done!



回答3:

Project Properties -> Resources -> Add Resource -> Add New Icon/Add Existing File (depending on whether or not you've already made the icon)



回答4:

I've just created a simple tool to do exactly this without having to mess with .res files. (If you manage your own .res files you won't get a manifest and version resource created automatically). It is a tiny utility which you can use as part of your Post-Build event and lets you add all icon files in a particular folder to your assembly. If we assume that you have a icons folder under your main project folder you can add the following post-build event:

C:\path\to\InsertIcons.exe $(TargetPath) $(ProjectDir)icons

A further description and a download can be found at http://einaregilsson.com/add-multiple-icons-to-a-dotnet-application/



回答5:

In addition to adding the files to your project resources, you can add the files to your project and set their Build Action to resource.

You can then refer to the files from XAML using the Source property. For example a file Icon.png in a directory "Resources" would be loaded using:

<Image Source="/Resources/Icon.png"/>

And to load the file from code you would use:

new BitmapImage(new Uri(@"pack://application:,,,/Resources/Icon.png"))


标签: c# wpf xaml icons