Icon from fle extension

M

Matt

How do I get a Icon for display in a list view from just the file extension?

If I have a file that exists on a local machine I can use
Icon.ExtractAssociatedIcon, but in this case the files are not on the
machine. The extension may give the incorrect informaton about file type,
but it is all the information I have.

There seems to be some information in the registry about this, but will I
always have permission to view this information in the registery?
 
N

Nicholas Paldino [.NET/C# MVP]

Matt,

Due to things like shell extensions, the icon that can be returned might
not be what you expect based on the registry information. This is an
implementation detail, and should be avoided.

Rather, what you want to do is call the SHGetFileInfo function in the
windows API through the P/Invoke layer (you can get the definition from
http://www.pinvoke.net). You can pass it a wildcard for the filename (with
extension) and it will return you the icon for that file (you might possibly
get a more specific icon by passing the complete path of the file).
 
M

Matt

I have tried to do this. It works fine for files that exist that I have
found, but I cannot get the wildcards to work. What do the wildcards for
this api look like?
".gif" does not work. ". "*.gif" does not work. But, "C:\logs.log" works
fine.


Nicholas Paldino said:
Matt,

Due to things like shell extensions, the icon that can be returned
might not be what you expect based on the registry information. This is
an implementation detail, and should be avoided.

Rather, what you want to do is call the SHGetFileInfo function in the
windows API through the P/Invoke layer (you can get the definition from
http://www.pinvoke.net). You can pass it a wildcard for the filename
(with extension) and it will return you the icon for that file (you might
possibly get a more specific icon by passing the complete path of the
file).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Matt said:
How do I get a Icon for display in a list view from just the file
extension?

If I have a file that exists on a local machine I can use
Icon.ExtractAssociatedIcon, but in this case the files are not on the
machine. The extension may give the incorrect informaton about file
type, but it is all the information I have.

There seems to be some information in the registry about this, but will I
always have permission to view this information in the registery?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top