I was looking for a C # library that gets an icon of a given path with different sizes, finally, when I got exactly the class that I need. He has a problem:
This method gets an icon of the given path:
public static BitmapSource GetIcon(string FileName, bool small, bool checkDisk, bool addOverlay) { SHFILEINFO shinfo = new SHFILEINFO(); uint SHGFI_USEFILEATTRIBUTES = 0x000000010; uint SHGFI_LINKOVERLAY = 0x000008000; uint flags; if (small) { flags = SHGFI_ICON | SHGFI_SMALLICON; } else { flags = SHGFI_ICON | SHGFI_LARGEICON; } if (!checkDisk) flags |= SHGFI_USEFILEATTRIBUTES; if (addOverlay) flags |= SHGFI_LINKOVERLAY; var res = SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), flags); if (res == 0) { throw (new System.IO.FileNotFoundException()); } var ico = System.Drawing.Icon.FromHandle(shinfo.hIcon);
The previous code, as in this question, works as expected, however AFTER , to get the file path icons in the directory successfully, it gives an exception in this line:
var ico = System.Drawing.Icon.FromHandle(shinfo.hIcon);
An exception of type 'System.IO.FileNotFoundException' occurred in WPF_REMOTE.exe but was not handled in user code
Additional information: Unable to find the specified file.
So why is this happening? Update: I found out that this was due to the fact that there was a path that contains unicode characters, and I need to use SHFILEINFOW, but I can not figure out how to change SHFILEINFO to SHFILEINFOW
another question about the CloseHandle(shinfo.hIcon); line CloseHandle(shinfo.hIcon); always gives an exception:
An exception of type 'System.Runtime.InteropServices.SEHException' occurred in WPF_REMOTE.exe but was not handled in user code
Additional information: External component has thrown an exception.
I wonder why it doesn't work! and why should I use it if this method already works without it.
also, if you have any improvements that I could use in this class, tell me, thanks in Advance.