I have a great technique to get the associated extension/image in the current system (because extensions can have different images from a system to another). And here is the function:
public static Icon getIconFromFile(string ext, bool large = true)
{
string fileName = (new Random()).Next(100, 1000).ToString() + ext;
System.IO.File.Create(fileName);
System.Drawing.Icon icon;
SHFILEINFO shinfo = new SHFILEINFO();
if (large)
{
IntPtr hImgLarge = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
else
{
IntPtr hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
try
{
System.IO.File.Delete(fileName);
}
catch(Exception e)
{
System.Console.WriteLine(e.StackTrace);
}
return icon;
}
The problem is that the function doesn't close the access to the file, so i can't remove it. How can i do ? Thanks