public static Icon GetIconFromFile(string FileName) { try { if (File.Exists(FileName) == false) { return(null); } IntPtr hImgSmall; //The handle to the system image list. //Dim hImgLarge As IntPtr 'The handle to the system image list. Native.SHFILEINFO shinfo; shinfo = new Native.SHFILEINFO(); shinfo.szDisplayName = new string('\0', 260); shinfo.szTypeName = new string('\0', 80); //Use this to get the small icon. hImgSmall = Native.SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), Convert.ToInt32(Native.SHGFI_ICON | Native.SHGFI_SMALLICON)); //Use this to get the large icon. //hImgLarge = SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON); //The icon is returned in the hIcon member of the //shinfo struct. Icon myIcon; myIcon = Icon.FromHandle(shinfo.hIcon); return(myIcon); } catch (Exception ex) { Runtime.MessageCollector.AddMessage(MessageClass.WarningMsg, (string) ("GetIconFromFile failed (Tools.Misc)" + Constants.vbNewLine + ex.Message), true); return(null); } }
public static void ParseShortcut(string filename, out Bitmap icon, out string displayName, out string targetPath, out string arguments) { Native.SHFILEINFO info = new Native.SHFILEINFO(true); int cbFileInfo = Marshal.SizeOf(info); Native.SHGFI flags; flags = Native.SHGFI.Icon | Native.SHGFI.DisplayName | Native.SHGFI.SysIconIndex; IntPtr sysImageList = Native.SHGetFileInfo(filename, 0, ref info, (uint)cbFileInfo, (uint)flags); displayName = info.szDisplayName; if (sysImageList != IntPtr.Zero && info.iIcon != 0) { icon = Icon.FromHandle(Native.ImageList_GetIcon(sysImageList, info.iIcon, 0)).ToBitmap(); } else { throw new Exception("Cannot pin !"); } // Create WshShellClass instance: IWshShell3 wshShell = new WshShellClass(); IWshRuntimeLibrary.IWshShortcut shorcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(filename); targetPath = shorcut.TargetPath; if (!string.IsNullOrWhiteSpace(shorcut.Arguments)) { arguments = shorcut.Arguments; } else { arguments = string.Empty; } }
public static Icon GetIconFromFile(string FileName) { try { if (File.Exists(FileName) == false) { return null; } IntPtr hImgSmall; //The handle to the system image list. //Dim hImgLarge As IntPtr 'The handle to the system image list. Native.SHFILEINFO shinfo; shinfo = new Native.SHFILEINFO(); shinfo.szDisplayName = new string('\0', 260); shinfo.szTypeName = new string('\0', 80); //Use this to get the small icon. hImgSmall = Native.SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), Convert.ToInt32(Native.SHGFI_ICON | Native.SHGFI_SMALLICON)); //Use this to get the large icon. //hImgLarge = SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON); //The icon is returned in the hIcon member of the //shinfo struct. Icon myIcon; myIcon = Icon.FromHandle(shinfo.hIcon); return myIcon; } catch (Exception ex) { Runtime.MessageCollector.AddMessage(MessageClass.WarningMsg, (string) ("GetIconFromFile failed (Tools.Misc)" + Constants.vbNewLine + ex.Message), true); return null; } }