icon associated with file

  • Thread starter Thread starter new.microsoft.com
  • Start date Start date
N

new.microsoft.com

How do I get the icon associated with a file??? I am trying to build a
file-explorer application and want to display the icon of each file.

Thanks.
 
I think I understand what you're asking: how do I know what icon is
associated with a file of a given type by Explorer so that I can display the
same icon as Explorer would? Is that what you're saying?

If so, the association of a file type (extension), with an icon is done
through the registry. For example, here's a sample that associates TXT
files with my application, cepad, and assigns an icon from that to them:

[HKEY_CLASSES_ROOT\.txt]
@="txtfile"
[HKEY_CLASSES_ROOT\txtfile]
@="Text File"
[HKEY_CLASSES_ROOT\txtfile\DefaultIcon]
@="\\Flash\\CEPad.exe,-101"
[HKEY_CLASSES_ROOT\txtfile\Shell\Open\Command]
@="\"\\Flash\\CEPad.exe\" %1"

The DefaultIcon item is the one that you'll need to parse. It gives the
location of a file, optionally followed by a comma and the resource ID
within the indicated file. In this case, the icon resource in cepad.exe
with ID = 101 (the minus sign is a bit of a mystery), is the one used by
Explorer to display the file.

You *may* be able to use SHGetFileInfo(..., SHGFI_ICON) to hide your access
to the registry items, also. You'd have to try that, to be sure.

Paul T.
 
How do I use this information to extract the icon into an Image, Bitmap or
Icon object so I can add to my image list and bind to my treeview? So how
do I get the icon for text files out of "\windows\pword.exe, -101" ???

Using the full framework I can use SHGetFileInfo to get the icon handle and
then create the icon. But the CF does have a function to create an icon
from a handle. Any ideas....

Thanks.

-Emad


Paul G. Tobey said:
I think I understand what you're asking: how do I know what icon is
associated with a file of a given type by Explorer so that I can display the
same icon as Explorer would? Is that what you're saying?

If so, the association of a file type (extension), with an icon is done
through the registry. For example, here's a sample that associates TXT
files with my application, cepad, and assigns an icon from that to them:

[HKEY_CLASSES_ROOT\.txt]
@="txtfile"
[HKEY_CLASSES_ROOT\txtfile]
@="Text File"
[HKEY_CLASSES_ROOT\txtfile\DefaultIcon]
@="\\Flash\\CEPad.exe,-101"
[HKEY_CLASSES_ROOT\txtfile\Shell\Open\Command]
@="\"\\Flash\\CEPad.exe\" %1"

The DefaultIcon item is the one that you'll need to parse. It gives the
location of a file, optionally followed by a comma and the resource ID
within the indicated file. In this case, the icon resource in cepad.exe
with ID = 101 (the minus sign is a bit of a mystery), is the one used by
Explorer to display the file.

You *may* be able to use SHGetFileInfo(..., SHGFI_ICON) to hide your access
to the registry items, also. You'd have to try that, to be sure.

Paul T.

new.microsoft.com said:
How do I get the icon associated with a file??? I am trying to build a
file-explorer application and want to display the icon of each file.

Thanks.
 
Sorry, I've only done this in C++, not in managed code.

Paul T.

King Tut said:
How do I use this information to extract the icon into an Image, Bitmap or
Icon object so I can add to my image list and bind to my treeview? So how
do I get the icon for text files out of "\windows\pword.exe, -101" ???

Using the full framework I can use SHGetFileInfo to get the icon handle and
then create the icon. But the CF does have a function to create an icon
from a handle. Any ideas....

Thanks.

-Emad


Paul G. Tobey said:
I think I understand what you're asking: how do I know what icon is
associated with a file of a given type by Explorer so that I can display the
same icon as Explorer would? Is that what you're saying?

If so, the association of a file type (extension), with an icon is done
through the registry. For example, here's a sample that associates TXT
files with my application, cepad, and assigns an icon from that to them:

[HKEY_CLASSES_ROOT\.txt]
@="txtfile"
[HKEY_CLASSES_ROOT\txtfile]
@="Text File"
[HKEY_CLASSES_ROOT\txtfile\DefaultIcon]
@="\\Flash\\CEPad.exe,-101"
[HKEY_CLASSES_ROOT\txtfile\Shell\Open\Command]
@="\"\\Flash\\CEPad.exe\" %1"

The DefaultIcon item is the one that you'll need to parse. It gives the
location of a file, optionally followed by a comma and the resource ID
within the indicated file. In this case, the icon resource in cepad.exe
with ID = 101 (the minus sign is a bit of a mystery), is the one used by
Explorer to display the file.

You *may* be able to use SHGetFileInfo(..., SHGFI_ICON) to hide your access
to the registry items, also. You'd have to try that, to be sure.

Paul T.

new.microsoft.com said:
How do I get the icon associated with a file??? I am trying to build a
file-explorer application and want to display the icon of each file.

Thanks.
 
Back
Top