Retrieve File Icon from Registry

  • Thread starter Thread starter mphanke
  • Start date Start date
M

mphanke

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
 
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...
 
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
 
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]
- (e-mail address removed)

mphanke said:
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
 
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."
 
Hi,

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

Thanks to all,

Martin
 
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."
 
Back
Top