如果我有一个文件的路径,有没有办法在 Windows 资源管理器中获取 Windows 为该文件显示的图标?
最佳答案
首先,图像有多种尺寸,其中一些尺寸比其他尺寸更容易获得。如果您想要“正常”大小的一号(我相信是 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;
}
}
关于C#:如何获取某个文件的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2160923/