public static extern IntPtr SHGetFileInfo( string pszPath, FILE_ATTRIBUTE dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, SHGetFileInfoConstants uFlags );
/// <summary> /// Returns the index of the icon for the specified file /// </summary> /// <param name="fileName">Filename to get icon for</param> /// <param name="forceLoadFromDisk">If True, then hit the disk to get the icon, /// otherwise only hit the disk if no cached icon is available.</param> /// <param name="iconState">Flags specifying the state of the icon /// returned.</param> /// <returns>Index of the icon</returns> public int IconIndex( string fileName, bool forceLoadFromDisk, ShellIconStateConstants iconState ) { SHGetFileInfoConstants dwFlags = SHGetFileInfoConstants.SHGFI_SYSICONINDEX; int dwAttr = 0; if (size == SysImageListSize.smallIcons) { dwFlags |= SHGetFileInfoConstants.SHGFI_SMALLICON; } // We can choose whether to access the disk or not. If you don't // hit the disk, you may get the wrong icon if the icon is // not cached. Also only works for files. if (!forceLoadFromDisk) { dwFlags |= SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES; dwAttr = FILE_ATTRIBUTE_NORMAL; } else { dwAttr = 0; } // sFileSpec can be any file. You can specify a // file that does not exist and still get the // icon, for example sFileSpec = "C:\PANTS.DOC" SHFILEINFO shfi = new SHFILEINFO(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); IntPtr retVal = SHGetFileInfo( fileName, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags) | (uint)iconState)); if (retVal.Equals(IntPtr.Zero)) { //System.Diagnostics.Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index"); if (forceLoadFromDisk) { if (System.IO.Directory.Exists(fileName)) { return(IconIndex(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), false, iconState)); } else { return(IconIndex(fileName, false, iconState)); } } else { return(0); } } else { return(shfi.iIcon); } }
/// <summary> /// Returns the index of the icon for the specified file /// </summary> /// <param name = "fileName">Filename to get icon for</param> /// <param name = "forceLoadFromDisk">If True, then hit the disk to get the icon, /// otherwise only hit the disk if no cached icon is available.</param> /// <param name = "iconState">Flags specifying the state of the icon /// returned.</param> /// <returns>Index of the icon</returns> public int IconIndex( string fileName, bool forceLoadFromDisk, ShellIconStateConstants iconState ) { SHGetFileInfoConstants dwFlags = SHGetFileInfoConstants.SHGFI_SYSICONINDEX | SHGetFileInfoConstants.SHGFI_DISPLAYNAME | SHGetFileInfoConstants.SHGFI_TYPENAME; int dwAttr = 0; if (size == SystemImageListSize.SmallIcons) { dwFlags |= SHGetFileInfoConstants.SHGFI_SMALLICON; } // We can choose whether to access the disk or not. If you don't // hit the disk, you may get the wrong icon if the icon is // not cached. Also only works for files. if (!forceLoadFromDisk) { dwFlags |= SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES; dwAttr = FILE_ATTRIBUTE_NORMAL; } else { dwAttr = 0; } // sFileSpec can be any file. You can specify a // file that does not exist and still get the // icon, for example sFileSpec = "C:\PANTS.DOC" SHFILEINFO shfi = new SHFILEINFO(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); IntPtr retVal = SHGetFileInfo( fileName, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags) | (uint)iconState)); if (retVal == IntPtr.Zero) { // Seems to be a special folder, so we need to try with the PIDL dwFlags |= SHGetFileInfoConstants.SHGFI_PIDL; IntPtr pidl = IntPtr.Zero; uint iAttribute; // Convert the folder name to the PIDL and get it's icon SHParseDisplayName(fileName, IntPtr.Zero, out pidl, 0, out iAttribute); if (pidl == IntPtr.Zero) { return(0); } retVal = SHGetFileInfo(pidl, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags) | (uint)iconState)); Marshal.FreeCoTaskMem(pidl); if (retVal != IntPtr.Zero) { return(shfi.iIcon); } return(0); } return(shfi.iIcon); }
private static int GetIconIndex(FileDetails file) { string extension = file.Extension; if (icons.ContainsKey(file.Extension)) { return(icons[file.Extension]); } SHGetFileInfoConstants flags = SHGetFileInfoConstants.SHGFI_ICON | SHGetFileInfoConstants.SHGFI_SMALLICON; SHFILEINFO shfi = new SHFILEINFO(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); Icon icon = null; int index = -1; int ret = SHGetFileInfo(file.Path, 0, ref shfi, shfiSize, (uint)(flags)); if ((ret != 0) && (shfi.hIcon != IntPtr.Zero)) { icon = Icon.FromHandle(shfi.hIcon); SmallIcons.Images.Add(icon); index = SmallIcons.Images.Count - 1; } flags = SHGetFileInfoConstants.SHGFI_ICON | SHGetFileInfoConstants.SHGFI_SHELLICONSIZE; shfi = new SHFILEINFO(); shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); ret = SHGetFileInfo(file.Path, 0, ref shfi, shfiSize, (uint)(flags)); if ((ret != 0) && (shfi.hIcon != IntPtr.Zero)) { icon = Icon.FromHandle(shfi.hIcon); LargeIcons.Images.Add(icon); if (index < 0) { SmallIcons.Images.Add(icon); index = SmallIcons.Images.Count - 1; } } else if (icon != null) { LargeIcons.Images.Add(icon); } if ((extension == "EXE") || (extension == "LNK")) { return(index); } if (extension.Length > 0) { icons[extension] = index; } return(index); }
/// <summary> /// Creates the SystemImageList /// </summary> void create() { // forget last image list if any: hIml = IntPtr.Zero; if (isXpOrAbove()) { // Get the System IImageList object from the Shell: Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); int ret = SHGetImageList((int)size, ref iidImageList, ref iImageList); // the image list handle is the IUnknown pointer, but // using Marshal.GetIUnknownForObject doesn't return // the right value. It really doesn't hurt to make // a second call to get the handle: SHGetImageListHandle((int)size, ref iidImageList, ref hIml); } else { // Prepare flags: SHGetFileInfoConstants dwFlags = SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES | SHGetFileInfoConstants.SHGFI_SYSICONINDEX; if (size == SysImageListSize.smallIcons) { dwFlags |= SHGetFileInfoConstants.SHGFI_SMALLICON; } // Get image list SHFILEINFO shfi = new SHFILEINFO(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); // Call SHGetFileInfo to get the image list handle // using an arbitrary file: hIml = SHGetFileInfo(IntPtr.Zero, FILE_ATTRIBUTE_NORMAL, ref shfi, shfiSize, (uint)dwFlags); System.Diagnostics.Debug.Assert((hIml != IntPtr.Zero), "Failed to create Image List"); } }
/// <summary> /// Constructs a new, default instance of the FileIcon /// class. Specify the filename and call GetInfo() /// to retrieve an icon. /// </summary> public FileIcon() { flags = SHGetFileInfoConstants.SHGFI_ICON | SHGetFileInfoConstants.SHGFI_DISPLAYNAME | SHGetFileInfoConstants.SHGFI_TYPENAME | SHGetFileInfoConstants.SHGFI_ATTRIBUTES | SHGetFileInfoConstants.SHGFI_EXETYPE; }
/// <summary> /// Returns the index of the icon for the specified file /// </summary> /// <param name="fileName">Filename to get icon for</param> /// <param name="forceLoadFromDisk">If True, then hit the disk to get the icon, /// otherwise only hit the disk if no cached icon is available.</param> /// <param name="iconState">Flags specifying the state of the icon /// returned.</param> /// <returns>Index of the icon</returns> static int IconIndex(string fileName, bool forceLoadFromDisk) { SHGetFileInfoConstants dwFlags = SHGetFileInfoConstants.SHGFI_SYSICONINDEX; int dwAttr = 0; SHFILEINFO shfi = new SHFILEINFO(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); IntPtr retVal = SHGetFileInfo(fileName, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags))); if (retVal.Equals(IntPtr.Zero)) { System.Diagnostics.Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index"); return(0); } else { return(shfi.iIcon); } }
/// <summary> /// Returns the index of the icon for the specified file /// </summary> /// <param name="fileName">Filename to get icon for</param> /// <param name="forceLoadFromDisk">If True, then hit the disk to get the icon, /// otherwise only hit the disk if no cached icon is available.</param> /// <param name="iconState">Flags specifying the state of the icon /// returned.</param> /// <returns>Index of the icon</returns> public int IconIndex(string fileName, bool forceLoadFromDisk, ShellIconStateConstants iconState) { SHGetFileInfoConstants flags = SHGetFileInfoConstants.SHGFI_SYSICONINDEX; int attr = 0; if (this.size == SysImageListSize.smallIcons) { flags |= SHGetFileInfoConstants.SHGFI_SMALLICON; } // We can choose whether to access the disk or not. If you don't // hit the disk, you may get the wrong icon if the icon is // not cached. Also only works for files. if (!forceLoadFromDisk) { flags |= SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES; attr = FILEATTRIBUTENORMAL; } else { attr = 0; } // sFileSpec can be any file. You can specify a // file that does not exist and still get the // icon, for example sFileSpec = "C:\PANTS.DOC" SHFILEINFO shfi = new SHFILEINFO(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); IntPtr retVal = SHGetFileInfo(fileName, attr, ref shfi, shfiSize, (uint)flags | (uint)iconState); if (retVal.Equals(IntPtr.Zero)) { System.Diagnostics.Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index"); return(0); } else { return(shfi.Icon); } }
public int IconIndexFromExt(string extension) { SHGetFileInfoConstants dwFlags = SHGetFileInfoConstants.SHGFI_SYSICONINDEX; int dwAttr = 0; if (size == SysImageListSize.smallIcons) { dwFlags |= SHGetFileInfoConstants.SHGFI_SMALLICON; } // We can choose whether to access the disk or not. If you don't // hit the disk, you may get the wrong icon if the icon is // not cached. Also only works for files. //if (!forceLoadFromDisk) //{ dwFlags |= SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES; dwAttr = FILE_ATTRIBUTE_NORMAL; //} //else //{ // dwAttr = 0; ///} dwFlags |= SHGetFileInfoConstants.SHGFI_PIDL; // sFileSpec can be any file. You can specify a // file that does not exist and still get the // icon, for example sFileSpec = "C:\PANTS.DOC" SHFILEINFO shfi = new SHFILEINFO(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); IntPtr retVal = SHGetFileInfo( extension, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags) | (uint)ShellIconStateConstants.ShellIconStateNormal)); if (retVal.Equals(IntPtr.Zero)) { dwAttr = 0; retVal = SHGetFileInfo(extension, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags) | (uint)ShellIconStateConstants.ShellIconStateNormal)); } return(shfi.iIcon); }
private FileDetails(FileInfo file) { this.file = file; typeName = ""; displayName = ""; SHGetFileInfoConstants flags = SHGetFileInfoConstants.SHGFI_DISPLAYNAME | SHGetFileInfoConstants.SHGFI_TYPENAME; SHFILEINFO shfi = new SHFILEINFO(); uint shfiSize = (uint)Marshal.SizeOf(shfi.GetType()); int ret = SHGetFileInfo(Path, 0, ref shfi, shfiSize, (uint)(flags)); if (ret != 0) { typeName = shfi.szTypeName; displayName = shfi.szDisplayName; } iconIndex = GetIconIndex(this); }
public static FileIcon FromFile(string FileName, SHGetFileInfoConstants Flags = Flags_Default) { var ret = default(FileIcon); if (Win32.SHGetFileInfo(FileName, 0, (uint)(Flags), out var shfi) == IntPtr.Zero) { var ErrorNumber = Win32.GetLastError(); var ErrorMessage = Win32.FormatMessage(FormatMessageFlags.FROM_SYSTEM | FormatMessageFlags.IGNORE_INSERTS, IntPtr.Zero, ErrorNumber, 0, IntPtr.Zero); var ExceptionMessage = $@"Error #{ErrorNumber}: {ErrorMessage}"; throw new Exception(ExceptionMessage); } else { ret = new FileIcon() { TypeName = shfi.szTypeName, DisplayName = shfi.szDisplayName, ShellIcon = shfi.hIcon == IntPtr.Zero ? null : System.Drawing.Icon.FromHandle(shfi.hIcon) }; } return(ret); }
/// <summary> /// Constructs a new instance of the FileIcon class /// and retrieves the information specified in the /// flags. /// </summary> /// <param name="fileName">The filename to get information /// for</param> /// <param name="flags">The flags to use when extracting the /// icon and other shell information.</param> public FileIcon(string fileName, FileIcon.SHGetFileInfoConstants flags) { this.fileName = fileName; this.flags = flags; GetInfo(); }
/// <summary> /// Constructs a new instance of the FileIcon class /// and retrieves the information specified in the /// flags. /// </summary> /// <param name="fileName"> /// The filename to get information /// for /// </param> /// <param name="flags"> /// The flags to use when extracting the /// icon and other shell information. /// </param> public FileIcon(string fileName, SHGetFileInfoConstants flags) { FileName = fileName; Flags = flags; GetInfo(); }
public FileIcon(string fileName, SHGetFileInfoConstants flags) { FileName = fileName; Flags = flags; GetInfo(); }
private void getAttributes(bool isDirectory, bool forceLoadFromDisk, out int dwAttr, out SHGetFileInfoConstants dwFlags) { dwFlags = SHGetFileInfoConstants.SHGFI_SYSICONINDEX; dwAttr = 0; if (_size == IconSize.small) { dwFlags |= SHGetFileInfoConstants.SHGFI_SMALLICON; } if (isDirectory) { dwAttr = FILE_ATTRIBUTE_DIRECTORY; } else if (!forceLoadFromDisk) { dwFlags |= SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES; dwAttr = FILE_ATTRIBUTE_NORMAL; } }
public static extern IntPtr SHGetFileInfo( IntPtr ppidl, FILE_ATTRIBUTE dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, SHGetFileInfoConstants uFlags);