Groups | Blog | Home
all groups > dotnet framework > may 2008 >

dotnet framework : How to get and use the icon of exe-files (programs) in own applications


Rolf Welskes
5/31/2008 3:00:35 AM
Hello,

I have in windows forms, on the other hand in windows presentation the
following problem:

I have a program with a list of programs (exe-files).
Now I want to display the (standard) icons of these programs, similiar as in
explorer.

So, for example I have the pathes:

f:/aaaa/bbb/office/excel.exe or
f:/aaa/ccc/adobe/premiere.exe

So I have access to the program files,
but how do I get the (standard) icons of these program files and how do I
use
it in windows forms and in windows presentation foundation?


Thank you for any help.

Best Regards
Rolf Welskes

Josip Medved
5/31/2008 8:06:42 AM
Hi,

you may want to use interop here.

E.g. (works for executables)
--
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)) {
Icon icon = System.Drawing.Icon.FromHandle(hIcon);
}
}
--

private static class NativeMethods {

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet =
CharSet.Unicode)]
static extern internal System.IntPtr LoadIcon(System.IntPtr hInstance,
string lpIconName);

[System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet =
CharSet.Unicode)]
static extern internal System.IntPtr LoadLibrary(string lpFileName);

}




[quoted text, click to view]
Josip Medved
5/31/2008 8:14:43 AM
BTW. Don't forget to do FreeLibrary like I just did. :)
Icon dispose would also be nice after you are done with it.

[quoted text, click to view]
Rolf Welskes
6/12/2008 1:17:26 AM
Hello,
thank you, but does not work,
debugging show, for example if i take as file name excel.exe,
Module is found, all, ok,
but:
System.IntPtr hIcon = NativeMethods.LoadIcon(hLibrary,
"#32512");

gives back zero.
So, it does not work.
Furthermore the problem seems to be "#32512" .
Think I want to build a tree like explorer and have all icons of all
applications, normal windows exe, exe from dotnet, exe from wpf
applications,
so as explorer get the icons.

Thank you for any help.

Rolf Welskes


"Josip Medved" <jmedved@jmedved.com> schrieb im Newsbeitrag
news:C0D2E100-3E42-4C2B-A2D7-2096C64D6DFC@microsoft.com...
[quoted text, click to view]

AddThis Social Bookmark Button