Reusing the .NET Application Icon

How can I reuse the application icon from my application, so I do not need to insert it twice (once for the application icon and once for internal use)?

+5
source share
2 answers

You can read it back through P / Interop calls. This happens something like this:

static Icon GetAppIcon() {
    var fileName = Assembly.GetEntryAssembly().Location
    System.IntPtr hLibrary = NativeMethods.LoadLibrary(fileName);
    if (!hLibrary.Equals(System.IntPtr.Zero)) {
        System.IntPtr hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
        if (!hIcon.Equals(System.IntPtr.Zero)) {
            return Icon.FromHandle(hIcon);
        }
    }
    return null; //no icon was retrieved
}

In addition, native signatures:

private static class NativeMethods {
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    static extern internal IntPtr LoadIcon(IntPtr hInstance, string lpIconName);

    [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
     static extern internal IntPtr LoadLibrary(string lpFileName);
}
+4
source

It seems the easiest way to use Icon.ExtractAssociatedIcon as described in this related question: Prevent duplication of icon resources in a .NET project (C #)

+6

All Articles