Embed multiple icons in WPF EXE

前端 未结 5 808
后悔当初
后悔当初 2020-12-30 12:38

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?

@sm

相关标签:
5条回答
  • 2020-12-30 13:06

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

    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-12-30 13:08

    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...

    0 讨论(0)
  • 2020-12-30 13:18
    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!

    0 讨论(0)
  • 2020-12-30 13:25

    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"))
    
    0 讨论(0)
提交回复
热议问题