public static BitmapSource FileAssociatedImage(string path, bool isLarge, bool isExist)
{
var fileInfo = new SHFILEINFO();
uint flags = SHGFI_ICON;
if (!isLarge) flags |= SHGFI_SMALLICON;
if (!isExist) flags |= SHGFI_USEFILEATTRIBUTES;
try
{
SHGetFileInfo(path, 0, ref fileInfo, (uint)Marshal.SizeOf(fileInfo), flags);
if (fileInfo.hIcon == IntPtr.Zero)
{
return null;
}
else
{
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(fileInfo.hIcon, new Int32Rect(0, 0, 16, 16), BitmapSizeOptions.FromEmptyOptions());
}
}
finally
{
if (fileInfo.hIcon != IntPtr.Zero)
{
DestroyIcon(fileInfo.hIcon);
}
}
}