public static Icon IconFromExtension(string Extension, IconSize Size) { try { Icon TempIcon; //add '.' if nessesry if (Extension[0] != '.') Extension = '.' + Extension; //temp struct for getting file shell info SHFILEINFO TempFileInfo = new SHFILEINFO(); SHGetFileInfo( Extension, 0, ref TempFileInfo, (uint)Marshal.SizeOf(TempFileInfo), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | (uint)Size); TempIcon = (Icon)Icon.FromHandle(TempFileInfo.hIcon); return GetManagedIcon(ref TempIcon); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("error while trying to get icon for " + Extension + " :" + e.Message); return null; } }
static extern IntPtr SHGetFileInfo( string pszPath, //path uint dwFileAttributes, //attributes ref SHFILEINFO psfi, //struct pointer uint cbSizeFileInfo, //size uint uFlags); //flags