private static IntPtr GetIconHandleFromFilePath(string filepath, IconSizeEnum iconsize) { var shinfo = new Shell.SHFILEINFO(); const uint SHGFI_SYSICONINDEX = 0x4000; const int FILE_ATTRIBUTE_NORMAL = 0x80; const uint flags = SHGFI_SYSICONINDEX; return(getIconHandleFromFilePathWithFlags(filepath, iconsize, ref shinfo, FILE_ATTRIBUTE_NORMAL, flags)); }
private static IntPtr GetIconHandleFromFolderPath(string folderpath, IconSizeEnum iconsize) { var shinfo = new Shell.SHFILEINFO(); const uint SHGFI_ICON = 0x000000100; const uint SHGFI_USEFILEATTRIBUTES = 0x000000010; const int FILE_ATTRIBUTE_DIRECTORY = 0x00000010; const uint flags = SHGFI_ICON | SHGFI_USEFILEATTRIBUTES; return(getIconHandleFromFilePathWithFlags(folderpath, iconsize, ref shinfo, FILE_ATTRIBUTE_DIRECTORY, flags)); }
public DirectoryNode(string path) { if (!Directory.Exists(path)) { throw new FileNotFoundException("The directory added to MediaDirectory was not found.", path); } _path = path; _nodes = new List <IMediaNode>(); _name = new FileInfo(_path).Name; string[] files = Directory.GetFiles(_path); foreach (string file in files) { _nodes.Add(new FileNode(file)); } string[] directories = Directory.GetDirectories(_path); foreach (string directory in directories) { _nodes.Add(new DirectoryNode(directory)); } _sizeOnDisc = 0; foreach (IMediaNode node in _nodes) { _sizeOnDisc += node.SizeOnDisc; } // // Get the Directory icon // Shell.SHFILEINFO shinfo = new Shell.SHFILEINFO(); IntPtr hImg = Shell.SHGetFileInfo(_path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Shell.SHGFI_ICON | Shell.SHGFI_SMALLICON); if (shinfo.hIcon != null) { //The icon is returned in the hIcon member of the shinfo struct System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter(); System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon); try { _fileIcon = (System.Drawing.Image)imageConverter.ConvertTo(icon, typeof(System.Drawing.Image)); } catch (NotSupportedException) { } Shell.DestroyIcon(shinfo.hIcon); } }
private static IntPtr getIconHandleFromFilePathWithFlags( string filepath, IconSizeEnum iconsize, ref Shell.SHFILEINFO shinfo, int fileAttributeFlag, uint flags) { const int ILD_TRANSPARENT = 1; var retval = SHGetFileInfo(filepath, fileAttributeFlag, ref shinfo, Marshal.SizeOf(shinfo), flags); if (retval == 0) { throw new System.IO.FileNotFoundException(); } var iconIndex = shinfo.iIcon; var iImageListGuid = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); _ = SHGetImageList((int)iconsize, ref iImageListGuid, out Shell.IImageList iml); var hIcon = IntPtr.Zero; _ = iml.GetIcon(iconIndex, ILD_TRANSPARENT, ref hIcon); return(hIcon); }
public FileNode(string path) { if (!File.Exists(path)) { throw new FileNotFoundException("The file added to MediaFile was not found.", path); } _fileIcon = null; _path = path; FileInfo fileInfo = new FileInfo(_path); _name = fileInfo.Name; _sizeOnDisc = fileInfo.Length; // // Get the File icon // Shell.SHFILEINFO shinfo = new Shell.SHFILEINFO(); IntPtr hImg = Shell.SHGetFileInfo(_path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Shell.SHGFI_ICON | Shell.SHGFI_SMALLICON); if (shinfo.hIcon != null) { //The icon is returned in the hIcon member of the shinfo struct System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter(); System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon); try { _fileIcon = (System.Drawing.Image)imageConverter.ConvertTo(icon, typeof(System.Drawing.Image)); } catch (NotSupportedException) { } Shell.DestroyIcon(shinfo.hIcon); } }
public static extern int SHGetFileInfo( string pszPath, int dwFileAttributes, ref Shell.SHFILEINFO psfi, int cbFileInfo, uint uFlags);