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().GetModules();
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
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