/// <summary> /// load computer display name and icon /// </summary> /// <param name="displayName"></param> /// <param name="computerIcon"></param> public static void LoadComputer(out string displayName, out ImageSource computerIcon) { try { IntPtr ptrIDL = IntPtr.Zero; var result = ShellAPI.SHGetSpecialFolderLocation(IntPtr.Zero, ShellAPI.CSIDL.CSIDL_DRIVES, ref ptrIDL); if (result != 0) { Marshal.ThrowExceptionForHR(result); } ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO(); ShellAPI.SHGFI vFlags = ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_ICON | ShellAPI.SHGFI.SHGFI_PIDL | ShellAPI.SHGFI.SHGFI_DISPLAYNAME | ShellAPI.SHGFI.SHGFI_TYPENAME | ShellAPI.SHGFI.SHGFI_ADDOVERLAYS; ShellAPI.SHGetFileInfo(ptrIDL, 0, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags); displayName = shellInfo.szDisplayName; computerIcon = GetImageSourceFromIcon(shellInfo.hIcon); if (shellInfo.hIcon != IntPtr.Zero) { User32API.DestroyIcon(shellInfo.hIcon); shellInfo.hIcon = IntPtr.Zero; } } catch (Exception ex) { displayName = string.Empty; computerIcon = null; LogHelper.Debug("Application Exception:", ex); } }
public static ImageSource LoadDriveIcon(string path) { if (path == null) { return(null); } //TT91027 - The folder's icon doesn't display in the content list while restore the backup job which target is FTP. //change ftp path '/' to '\' for XP. path = path.Replace('/', '\\'); ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO(); ShellAPI.SHGFI vFlags = ShellAPI.SHGFI.SHGFI_LARGEICON | ShellAPI.SHGFI.SHGFI_ICON | ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES | ShellAPI.SHGFI.SHGFI_DISPLAYNAME; ShellAPI.SHGetFileInfo(path, (uint)FileAttributes.Directory, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags); var ico = GetImageSourceFromIcon(shellInfo.hIcon); if (shellInfo.hIcon != IntPtr.Zero) { User32API.DestroyIcon(shellInfo.hIcon); shellInfo.hIcon = IntPtr.Zero; } return(ico); }
/// <summary> /// /// </summary> /// <param name="path"></param> /// <param name="attr"></param> /// <returns>image,displayname,typename</returns> public static Tuple <ImageSource, string, string> LoadFileInfoByPath(string path, FileAttributes attr = 0) { ImageSource icon = null; //TT91027 - The folder's icon doesn't display in the content list while restore the backup job which target is FTP. //change ftp path '/' to '\' for XP. path = path.Replace('/', '\\'); ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO(); ShellAPI.SHGFI vFlags = ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_ICON | ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES | ShellAPI.SHGFI.SHGFI_DISPLAYNAME | ShellAPI.SHGFI.SHGFI_TYPENAME; ShellAPI.SHGetFileInfo(path, (uint)attr, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags); string displayName = shellInfo.szDisplayName; if (shellInfo.hIcon != IntPtr.Zero) { icon = GetImageSourceFromIcon(shellInfo.hIcon); User32API.DestroyIcon(shellInfo.hIcon); shellInfo.hIcon = IntPtr.Zero; } string typeName = shellInfo.szTypeName; return(new Tuple <ImageSource, string, string>(icon, displayName, typeName)); }
/// <summary> /// Constructor to get the system ImageList. /// </summary> private ShellImageList() { imageTable = new Hashtable(); ShellAPI.SHGFI uFlags = ShellAPI.SHGFI.USEFILEATTRIBUTES | ShellAPI.SHGFI.SYSICONINDEX | ShellAPI.SHGFI.SMALLICON; ShellAPI.SHFILEINFO sfiSmall = new ShellAPI.SHFILEINFO(); // get a handle to the system image list for small icons smallImageListHandle = ShellAPI.SHGetFileInfo( ".txt", ShellAPI.FILE_ATTRIBUTE.NORMAL, ref sfiSmall, ShellAPI.cbFileInfo, uFlags); uFlags = ShellAPI.SHGFI.USEFILEATTRIBUTES | ShellAPI.SHGFI.SYSICONINDEX | ShellAPI.SHGFI.LARGEICON; ShellAPI.SHFILEINFO sfiLarge = new ShellAPI.SHFILEINFO(); // get a handle to the system image list for large icons largeImageListHandle = ShellAPI.SHGetFileInfo( ".txt", ShellAPI.FILE_ATTRIBUTE.NORMAL, ref sfiLarge, ShellAPI.cbFileInfo, uFlags); }
/// <summary> /// Constructor for the desktop shell item. /// </summary> /// <param name="explorer"> /// Explorer class that uses the shell item. /// </param> /// <param name="pidl"> /// Pointer to the desktop PIDL. /// </param> /// <param name="folderPtr"> /// Pointer to the IShellFolder interface for the desktop. /// </param> internal ShellItem(ShellExplorer explorer, IntPtr pidl, IntPtr folderPtr) { this.explorer = explorer; parent = null; this.folderPtr = folderPtr; // get managed object this.folder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(folderPtr, typeof(IShellFolder)); subFiles = new ShellItemList(this); subFolders = new ShellItemList(this); relPidl = new PIDL(pidl, false); text = "Desktop"; path = "Desktop"; SetDesktopAttributes(); ShellAPI.SHFILEINFO info = new ShellAPI.SHFILEINFO(); ShellAPI.SHGetFileInfo(relPidl.Ptr, 0, ref info, ShellAPI.cbFileInfo, ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.TYPENAME | ShellAPI.SHGFI.SYSICONINDEX); type = info.szTypeName; ShellImageList.SetIconIndex(this, info.iIcon, false); ShellImageList.SetIconIndex(this, info.iIcon, true); }
/// <summary> /// Load file icon, file type from shell /// </summary> /// <param name="path">The path.</param> /// <param name="attributes">The attr.</param> public static System.Windows.Media.ImageSource LoadFileInfo(string path, out string typeName) { FileAttributes attributes; if (CommonProvider.Directory.Exists(path)) { attributes = FileAttributes.Directory; } else if (CommonProvider.File.Exists(path)) { attributes = FileAttributes.Normal; } else { typeName = string.Empty; return(null); } typeName = string.Empty; if (path == null) { return(null); } //TT91027 - The folder's icon doesn't display in the content list while restore the backup job which target is FTP. //change ftp path '/' to '\' for XP. path = path.Replace('/', '\\'); ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO(); ShellAPI.SHGFI vFlags = ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_ICON | ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES | ShellAPI.SHGFI.SHGFI_TYPENAME; ShellAPI.SHGetFileInfo(path, (uint)attributes, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags); var ico = GetImageSourceFromIcon(shellInfo.hIcon); //displayName = shellInfo.szDisplayName; if (shellInfo.hIcon != IntPtr.Zero) { User32API.DestroyIcon(shellInfo.hIcon); shellInfo.hIcon = IntPtr.Zero; } typeName = shellInfo.szTypeName; if (ico == null && attributes == FileAttributes.Directory) { ico = DefaultFolderImage; } return(ico); }
/// <summary> /// Retrieve the string that describes the file's type. /// </summary> private void SetInfo() { PIDL fullPidl = FullPidl; ShellAPI.SHFILEINFO info = new ShellAPI.SHFILEINFO(); ShellAPI.SHGetFileInfo(fullPidl.Ptr, 0, ref info, ShellAPI.cbFileInfo, ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.TYPENAME | ShellAPI.SHGFI.SYSICONINDEX); fullPidl.Free(); type = info.szTypeName; ShellImageList.SetIconIndex(this, info.iIcon, false); ShellImageList.SetIconIndex(this, info.iIcon, true); }
/// <summary> /// Load file information, dislpay name, icon, file type from shell /// </summary> /// <param name="pidl">The pidl.</param> void LoadFileInfo(IntPtr pidl) { ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO(); ShellAPI.SHGFI vFlags = ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_ICON | ShellAPI.SHGFI.SHGFI_PIDL | ShellAPI.SHGFI.SHGFI_DISPLAYNAME | ShellAPI.SHGFI.SHGFI_TYPENAME | ShellAPI.SHGFI.SHGFI_ADDOVERLAYS; ShellAPI.SHGetFileInfo(pidl, 0, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags); DisplayName = shellInfo.szDisplayName; if (shellInfo.hIcon != IntPtr.Zero) { Icon = GetImageSourceFromIcon(shellInfo.hIcon); User32API.DestroyIcon(shellInfo.hIcon); shellInfo.hIcon = IntPtr.Zero; } TypeName = shellInfo.szTypeName; }
/// <summary> /// Load file information, dislpay name, icon, file type from shell /// </summary> /// <param name="path">The path.</param> /// <param name="attr">The attr.</param> void LoadFileInfo(string path, FileAttributes attr) { //TT91027 - The folder's icon doesn't display in the content list while restore the backup job which target is FTP. //change ftp path '/' to '\' for XP. path = path.Replace('/', '\\'); ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO(); ShellAPI.SHGFI vFlags = ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_ICON | ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES | ShellAPI.SHGFI.SHGFI_DISPLAYNAME | ShellAPI.SHGFI.SHGFI_TYPENAME; ShellAPI.SHGetFileInfo(path, (uint)attr, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags); DisplayName = shellInfo.szDisplayName; if (shellInfo.hIcon != IntPtr.Zero) { Icon = GetImageSourceFromIcon(shellInfo.hIcon); ShellAPI.DestroyIcon(shellInfo.hIcon); shellInfo.hIcon = IntPtr.Zero; } TypeName = shellInfo.szTypeName; }
/// <summary> /// Load file icon /// </summary> /// <param name="path">The path.</param> /// <param name="attributes">The attr.</param> public static ImageSource LoadFileInfo(string path, FileAttributes attributes) { if (path == null) { return(null); } if (attributes == FileAttributes.Normal) { if (!CommonProvider.File.Exists(path)) { path = Path.GetFileName(path); } } //TT91027 - The folder's icon doesn't display in the content list while restore the backup job which target is FTP. //change ftp path '/' to '\' for XP. path = path.Replace('/', '\\'); ShellAPI.SHFILEINFO shellInfo = new ShellAPI.SHFILEINFO(); ShellAPI.SHGFI vFlags = ShellAPI.SHGFI.SHGFI_SMALLICON | ShellAPI.SHGFI.SHGFI_ICON | ShellAPI.SHGFI.SHGFI_USEFILEATTRIBUTES; ShellAPI.SHGetFileInfo(path, (uint)attributes, ref shellInfo, (uint)Marshal.SizeOf(shellInfo), vFlags); var ico = GetImageSourceFromIcon(shellInfo.hIcon); if (shellInfo.hIcon != IntPtr.Zero) { User32API.DestroyIcon(shellInfo.hIcon); shellInfo.hIcon = IntPtr.Zero; } if (ico == null && attributes == FileAttributes.Directory) { ico = DefaultFolderImage; } return(ico); }
/// <summary> /// Returns the icon index for the given ShellItem. /// </summary> /// <param name="item"> /// ShellItem for which the icon index is calculated. /// </param> /// <param name="selected"> /// True if the item is selected, false otherwise. /// </param> public int GetIconIndex(ShellItem item, bool selected) { bool hasOverlay = false; int index = item.ImageIndex; int res; ShellAPI.FILE_ATTRIBUTE dwFileAttrib = 0; ShellAPI.SHGFI uFlags = ShellAPI.SHGFI.ICON | ShellAPI.SHGFI.PIDL | ShellAPI.SHGFI.SYSICONINDEX; int key = item.ImageIndex << 8; // check for overlays if (item.IsLink) { key = key | 1; uFlags = uFlags | ShellAPI.SHGFI.LINKOVERLAY; hasOverlay = true; } if (item.IsShared) { key = key | 2; uFlags = uFlags | ShellAPI.SHGFI.ADDOVERLAYS; hasOverlay = true; } // not really an overlay, but handled the same if (selected) { key = key | 4; uFlags = uFlags | ShellAPI.SHGFI.OPENICON; hasOverlay = true; } if (imageTable.ContainsKey(key)) { res = (int)imageTable[key]; } // for non-overlay icons we already have the index else if (!hasOverlay && !item.IsHidden) { res = index; imageTable[key] = index; } // don't have icon index for an overlay else { if (item.IsFileSystem && !item.IsDisk && !item.IsFolder) { uFlags |= ShellAPI.SHGFI.USEFILEATTRIBUTES; dwFileAttrib = ShellAPI.FILE_ATTRIBUTE.NORMAL; } ShellAPI.SHFILEINFO sfiSmall = new ShellAPI.SHFILEINFO(); ShellAPI.SHGetFileInfo(item.FullPidl, dwFileAttrib, ref sfiSmall, ShellAPI.cbFileInfo, uFlags | ShellAPI.SHGFI.SMALLICON); ShellAPI.SHFILEINFO sfiLarge = new ShellAPI.SHFILEINFO(); ShellAPI.SHGetFileInfo(item.FullPidl, dwFileAttrib, ref sfiLarge, ShellAPI.cbFileInfo, uFlags | ShellAPI.SHGFI.LARGEICON); lock (padlock) { // add overlaid icon to the image list res = ShellAPI.ImageList_ReplaceIcon(smallImageListHandle, -1, sfiSmall.hIcon); ShellAPI.ImageList_ReplaceIcon(largeImageListHandle, -1, sfiLarge.hIcon); } ShellAPI.DestroyIcon(sfiSmall.hIcon); ShellAPI.DestroyIcon(sfiLarge.hIcon); imageTable[key] = res; } return(res); }