469,076 Members | 1,538 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,076 developers. It's quick & easy.

Retrieve File Icon from Registry

Hi,

how can I retrieve the icon for a registered file extension from the
registry and show it in my ListView?

Any hints appreciated,

Martin
Nov 16 '05 #1
6 7332
Applications normally store the icon in their exe file, not in the registry.
If the application has stored its path in the registry, and if it does store
an icon in its file that you can get out, you may be able to find its exe
file and extract the icon. But that's quite a lot of "if"s and "might"s...
"mphanke" wrote:
Hi,

how can I retrieve the icon for a registered file extension from the
registry and show it in my ListView?

Any hints appreciated,

Martin

Nov 16 '05 #2
Okay,

thanks for all replies, finally I got this:
Reuse it if you like it!

#region ExtractAssociatedIcon
[DllImport("shell32.dll")]
static extern IntPtr ExtractAssociatedIcon(IntPtr hinst, string file,
ref int index);

public static System.Drawing.Icon FindAssocIcon(string filename)
{
try
{
string ext = System.IO.Path.GetExtension(filename);

RegWin32.RegistryKey key =
RegWin32.Registry.ClassesRoot.OpenSubKey(ext);

if(key != null)
{
string val = (string)key.GetValue("");

if(val != null && val != string.Empty)
{
key = RegWin32.Registry.ClassesRoot.OpenSubKey(val);

if(key != null)
{
key = key.OpenSubKey("DefaultIcon");

if(key != null)
{
val = (string)key.GetValue("");

if(val != null && val != string.Empty)
{
string name;
int icoIndex = -1;
int index = val.LastIndexOf(',');

if(index > 0)
{
name = val.Substring(0, index);

if(index < val.Length-1)
{
icoIndex = Convert.ToInt32( val.Substring(index+1) );
}
}//index
else
name = val;

System.Reflection.Module [] mod =
System.Reflection.Assembly.GetCallingAssembly().Ge tModules();
IntPtr ptr =
ExtractAssociatedIcon(Marshal.GetHINSTANCE(mod[0]), name, ref icoIndex);

if(ptr != IntPtr.Zero)
{
return System.Drawing.Icon.FromHandle(ptr);
}
}
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message+"\n"+ex.InnerException) ;
}
return null;
}
#endregion
mphanke wrote:
Hi,

how can I retrieve the icon for a registered file extension from the
registry and show it in my ListView?

Any hints appreciated,

Martin

Nov 16 '05 #3
mphanke,

I would recommend that you use the SHGetFileInfo API function, as it
will take into account other things, such as shell extensions and the like,
which ExtractAssociatedIcon does not.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mphanke" <mp*****@nospam.nospam> wrote in message
news:uy**************@TK2MSFTNGP09.phx.gbl...
Okay,

thanks for all replies, finally I got this:
Reuse it if you like it!

#region ExtractAssociatedIcon
[DllImport("shell32.dll")]
static extern IntPtr ExtractAssociatedIcon(IntPtr hinst, string file, ref
int index);

public static System.Drawing.Icon FindAssocIcon(string filename)
{
try
{
string ext = System.IO.Path.GetExtension(filename);

RegWin32.RegistryKey key = RegWin32.Registry.ClassesRoot.OpenSubKey(ext);

if(key != null)
{
string val = (string)key.GetValue("");

if(val != null && val != string.Empty)
{
key = RegWin32.Registry.ClassesRoot.OpenSubKey(val);

if(key != null)
{
key = key.OpenSubKey("DefaultIcon");

if(key != null)
{
val = (string)key.GetValue("");

if(val != null && val != string.Empty)
{
string name;
int icoIndex = -1;
int index = val.LastIndexOf(',');

if(index > 0)
{
name = val.Substring(0, index);

if(index < val.Length-1)
{
icoIndex = Convert.ToInt32( val.Substring(index+1) );
}
}//index
else
name = val;

System.Reflection.Module [] mod =
System.Reflection.Assembly.GetCallingAssembly().Ge tModules();
IntPtr ptr = ExtractAssociatedIcon(Marshal.GetHINSTANCE(mod[0]), name, ref
icoIndex);

if(ptr != IntPtr.Zero)
{
return System.Drawing.Icon.FromHandle(ptr);
}
}
}
}
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message+"\n"+ex.InnerException) ;
}
return null;
}
#endregion
mphanke wrote:
Hi,

how can I retrieve the icon for a registered file extension from the
registry and show it in my ListView?

Any hints appreciated,

Martin

Nov 16 '05 #4
Hi Martin,

Thanks for posting your code here. I'd like to know if this issue has been
resolved yet. Is there anything that I can help. I'm still monitoring on
it. If you have any questions, please feel free to post them in the
community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #5
Hi,

with the help of community members this issue was resolved yesterday ;-)

Thanks to all,

Martin

Kevin Yu [MSFT] wrote:
Hi Martin,

Thanks for posting your code here. I'd like to know if this issue has been
resolved yet. Is there anything that I can help. I'm still monitoring on
it. If you have any questions, please feel free to post them in the
community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #6
Hi Martin,

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Randy Crockett | last post: by
1 post views Thread by Víctor | last post: by
1 post views Thread by Jesper | last post: by
reply views Thread by Carl Mercier | last post: by
4 posts views Thread by marco.nl | last post: by
5 posts views Thread by IcingDeath via DotNetMonster.com | last post: by
9 posts views Thread by Rotzooi | last post: by
2 posts views Thread by =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post: by
5 posts views Thread by =?Utf-8?B?TG9hbldvbGY=?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.