如果我有一个文件的路径,是否有办法获取Windows Explorer中显示该文件所用的图标?
首先,图像有多种尺寸,其中一些比其他尺寸更容易获取。如果您想要“正常”大小的图像(我相信是32x32),并且可以使用WPF(引用PresentationCore),那么这是一个很好的方法:
public System.Windows.Media.ImageSource Icon
{
get
{
if (icon == null && System.IO.File.Exists(Command))
{
using (System.Drawing.Icon sysicon = System.Drawing.Icon.ExtractAssociatedIcon(Command))
{
// This new call in WPF finally allows us to read/display 32bit Windows file icons!
icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
sysicon.Handle,
System.Windows.Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
}
}
return icon;
}
}
System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filePath);
System.Windows.Interop.Imaging
类位于PresentationCore.dll
中,通常在 WPF 项目中引用。 - Jake Berger