Exemple #1
0
            /// <summary>
            /// Retrieves system icon and type description for specified file system path.
            /// </summary>
            /// <param name="path">File system path.</param>
            /// <param name="attr">File attributes, if null, file attributes will be read from path.</param>
            /// <param name="iconSize">Returned icon size.</param>
            /// <returns>File icon and type structure.</returns>
            public static FileIconAndType GetFileIconAndType(string path, FileAttributes?attr = null, SystemIconSize iconSize = SystemIconSize.Small)
            {
                if (path != null && path[1] == ':' && path.Length == 2)
                {
                    path += @"\";
                }
                var shFileInfo = new NativeMethods.SHFILEINFO();
                int cbFileInfo = Marshal.SizeOf(shFileInfo);
                var flags      = NativeMethods.SHGFI.TypeName;

                if (attr != null && path.Length > 3)
                {
                    flags |= NativeMethods.SHGFI.UseFileAttributes;
                }
                switch (iconSize)
                {
                case SystemIconSize.Small: flags |= NativeMethods.SHGFI.Icon | NativeMethods.SHGFI.SmallIcon; break;

                case SystemIconSize.Medium: flags |= NativeMethods.SHGFI.Icon; break;

                case SystemIconSize.Large: flags |= NativeMethods.SHGFI.Icon | NativeMethods.SHGFI.LargeIcon; break;
                }
                NativeMethods.SHGetFileInfo(path, (int)attr, out shFileInfo, (uint)cbFileInfo, flags);
                return(new FileIconAndType
                {
                    Icon = (shFileInfo.hIcon != IntPtr.Zero) ? GetImageFromHIcon(shFileInfo.hIcon) : null,
                    TypeDescription = shFileInfo.szTypeName
                });
            }
Exemple #2
0
        private static System.Drawing.Icon GetFileIcon(string name, bool getLargeIcon, bool linkOverlay)
        {
            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
            uint flags = NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES;

            if (true == linkOverlay)
            {
                flags += NativeMethods.SHGFI_LINKOVERLAY;
            }

            /* Check the size specified for return. */
            if (getLargeIcon == false)
            {
                flags += NativeMethods.SHGFI_SMALLICON; // include the small icon flag
            }
            else
            {
                flags += NativeMethods.SHGFI_LARGEICON;  // include the large icon flag
            }

            NativeMethods.SHGetFileInfo(name, NativeMethods.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi), flags);

            // Copy (clone) the returned icon to a new object, thus allowing us
            // to call DestroyIcon immediately
            System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone();
            NativeMethods.DestroyIcon(shfi.hIcon); // Cleanup

            return(icon);
        }
Exemple #3
0
        public static Icon IconFromExtension(string Extension, IconSize Size)
        {
            try
            {
                Icon TempIcon;

                //add '.' if nessesry
                if (Extension[0] != '.')
                {
                    Extension = '.' + Extension;
                }

                //temp struct for getting file shell info
                NativeMethods.SHFILEINFO TempFileInfo = new NativeMethods.SHFILEINFO();

                NativeMethods.SHGetFileInfo(
                    Extension,
                    0,
                    ref TempFileInfo,
                    (uint)Marshal.SizeOf(TempFileInfo),
                    SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | (uint)Size);

                TempIcon = (Icon)Icon.FromHandle(TempFileInfo.hIcon);
                return(GetManagedIcon(ref TempIcon));
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("error while trying to get icon for " + Extension + " :" + e.Message);
                return(null);
            }
        }
Exemple #4
0
        public int AddShellIcon(string path, ShellIconType flags)
        {
            var info = new NativeMethods.SHFILEINFO();

            try
            {
                var imageList = NativeMethods.SHGetFileInfo(
                    path, 0, ref info, (uint)Marshal.SizeOf(info),
                    ((uint)flags | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_SYSICONINDEX)
                    & ~NativeMethods.SHGFI_ICON
                    );

                if (imageList == IntPtr.Zero)
                {
                    return(-1);
                }

                if (imageList != _handle)
                {
                    throw new ArgumentException("Could not get image", "path");
                }

                return(info.iIcon);
            }
            finally
            {
                if (info.hIcon != IntPtr.Zero)
                {
                    NativeMethods.DestroyIcon(info.hIcon);
                }
            }
        }
Exemple #5
0
 public static string GetDescription(this FileSystemInfo info)
 {
     NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
        NativeMethods.SHGetFileInfo(info.FullName, 0, ref shfi, Marshal.SizeOf(shfi),
     NativeMethods.SHGetFileInfoFlags.SHGFI_DISPLAYNAME);
        return shfi.szDisplayName;
 }
Exemple #6
0
    void InitializeObject()
    {
        NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
        IntPtr result = IntPtr.Zero;

        try
        {
            result = NativeMethods.SHGetFileInfo(this.pidl, 0, ref shfi, Marshal.SizeOf(shfi),
                                                 NativeMethods.SHGFI.SHGFI_PIDL |
                                                 NativeMethods.SHGFI.SHGFI_DISPLAYNAME |
                                                 NativeMethods.SHGFI.SHGFI_ATTRIBUTES |
                                                 NativeMethods.SHGFI.SHGFI_TYPENAME
                                                 );
        }
        catch
        {
            System.Diagnostics.Debug.Write("Error in PIDL.InitializeObject");
        }
        displayName = shfi.szDisplayName;
        iconIndex   = shfi.iIcon;
        typeName    = shfi.szTypeName;

        m_attributes = (NativeMethods.SFGAO)shfi.dwAttributes;
        StringBuilder sb = new StringBuilder(260);

        NativeMethods.SHGetPathFromIDList(this.pidl, sb);
        physicalPath = sb.ToString();
    }
Exemple #7
0
 /// <summary>
 /// Uses SHGetFileInfo to retrieve the description for the given file,
 /// folder or drive.
 /// </summary>
 /// <param name="info">The file system object to query the description of.</param>
 /// <returns>A string containing the description</returns>
 public static string GetDescription(this FileSystemInfo info)
 {
     NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
     NativeMethods.SHGetFileInfo(info.FullName, 0, ref shfi, Marshal.SizeOf(shfi),
                                 NativeMethods.SHGetFileInfoFlags.SHGFI_DISPLAYNAME);
     return(shfi.szDisplayName);
 }
Exemple #8
0
        private int GetIconIndex(string fileName, bool isDirectory, bool forceLoadFromDisk, ShellIconStateConstants iconState)
        {
            NativeMethods.SHGetFileInfoConstants dwFlags; uint dwAttr;
            GetAttributes(isDirectory, forceLoadFromDisk, out dwAttr, out dwFlags);

            // sFileSpec can be any file.

            if (fileName.EndsWith(".lnk", StringComparison.InvariantCultureIgnoreCase))
            {
                dwFlags          |= NativeMethods.SHGetFileInfoConstants.SHGFI_LINKOVERLAY | NativeMethods.SHGetFileInfoConstants.SHGFI_ICON;
                iconState         = ShellIconStateConstants.ShellIconStateLinkOverlay;
                forceLoadFromDisk = true;
            }

            var shfi     = new NativeMethods.SHFILEINFO();
            var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());
            var retVal   = NativeMethods.SHGetFileInfo(fileName, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags) | (uint)iconState));

            if (!retVal.Equals(IntPtr.Zero))
            {
                return(shfi.iIcon.ToInt32());
            }
            if (forceLoadFromDisk)
            {
                return(GetIconIndex(Path.GetFileName(fileName), isDirectory, false, iconState));
            }
            Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index");
            return(-1);
        }
Exemple #9
0
        public static IntPtr Test()
        {
            var shfi     = new NativeMethods.SHFILEINFO();
            var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());

            return(NativeMethods.SHGetFileInfo(@"C:\", 16, ref shfi, shfiSize, 16384));
        }
    public static void Main(string[] args)
    {
        NativeMethods.SHFILEINFO info = new NativeMethods.SHFILEINFO();
        string fileName         = @"C:\Some\Path\SomeFile.png";
        uint   dwFileAttributes = NativeMethods.FILE_ATTRIBUTE.FILE_ATTRIBUTE_NORMAL;
        uint   uFlags           = (uint)(NativeMethods.SHGFI.SHGFI_TYPENAME | NativeMethods.SHGFI.SHGFI_USEFILEATTRIBUTES);

        NativeMethods.SHGetFileInfo(fileName, dwFileAttributes, ref info, (uint)Marshal.SizeOf(info), uFlags);
        Console.WriteLine(info.szTypeName);
    }
        protected void Initialize()
        {
            m_pidl = ConvertToPidl();
            NativeMethods.SHFILEINFO sfi = new NativeMethods.SHFILEINFO();
            NativeMethods.Shell32.SHGetFileInfo(m_pidl, 0, ref sfi, NativeMethods.cbFileInfo,
                                                NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_DISPLAYNAME);

            m_originalText = sfi.szDisplayName;
            m_customText   = string.Empty;
        }
Exemple #12
0
        public static Image GetImage(IntPtr pidl)
        {
            uint dwflag = NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_ICON;
            int  dwAttr = 0;

            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
            NativeMethods.Shell32.SHGetFileInfo(pidl, dwAttr, ref shfi, Marshal.SizeOf(shfi), dwflag);

            IntPtr iconPtr = NativeMethods.ComCtl32.ImageList_GetIcon(m_largeImageListHandle, shfi.iIcon, NativeMethods.ILD_NORMAL);

            return(Icon.FromHandle(iconPtr).ToBitmap());
        }
Exemple #13
0
        static string GetTypeName(string path)
        {
            NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
            IntPtr rslt = NativeMethods.SHGetFileInfoW(path, 0, ref fileinfo,
                                                       (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_TYPENAME);

            if (rslt == IntPtr.Zero)
            {
                return(null);
            }

            return(fileinfo.szTypeName);
        }
Exemple #14
0
 public static Icon GetIcon(this FileSystemInfo info)
 {
     NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
        NativeMethods.SHGetFileInfo(info.FullName, 0, ref shfi, Marshal.SizeOf(shfi),
     NativeMethods.SHGetFileInfoFlags.SHGFI_SMALLICON |
     NativeMethods.SHGetFileInfoFlags.SHGFI_ICON);
        if (shfi.hIcon != IntPtr.Zero)
     return Icon.FromHandle(shfi.hIcon);
        else
     throw new IOException(string.Format(CultureInfo.CurrentCulture,
      "Could not load file icon from {0}", info.FullName),
      Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()));
 }
Exemple #15
0
        /// <summary>
        /// Returns icon handle (small or large) for specified file.
        /// </summary>
        /// <param name="pidl">Source object for which icon should be retrieved.</param>
        /// <param name="bSmall">If value of the parameter is true small icon handle will be returned, large icon handle otherwise.</param>
        /// <returns>Returns icon handle for specified file.</returns>
        public static IntPtr GetIcon(Pidl pidl, bool small)
        {
            NativeMethods.SHFILEINFO info = new NativeMethods.SHFILEINFO(true);
            int cbFileInfo = Marshal.SizeOf(info);

            uint flags = NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_USEFILEATTRIBUTES | (small ? NativeMethods.SHGFI_SMALLICON : NativeMethods.SHGFI_LARGEICON);

            if (NativeMethods.SHGetFileInfo(pidl.Handle, 0, out info, (uint)cbFileInfo, flags) == IntPtr.Zero)
            {
                return(IntPtr.Zero);
            }

            return(info.hIcon);
        }
Exemple #16
0
        public SystemImageList()
        {
            var fileInfo = new NativeMethods.SHFILEINFO();

            _handle = NativeMethods.SHGetFileInfo(
                ".txt",
                0,
                ref fileInfo,
                (uint)Marshal.SizeOf(fileInfo),
                NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON
                );

            Debug.Assert(_handle != IntPtr.Zero);
        }
Exemple #17
0
        private static Icon GetIcon(string filePath, uint flags)
        {
            Contract.Requires(filePath != null);

            var shinfo = new NativeMethods.SHFILEINFO();

            if (NativeMethods.SHGetFileInfo(filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), NativeMethods.SHGFI_ICON | flags).ToInt32() > 0)
            {
                var icon = Icon.FromHandle(shinfo.hIcon).Clone() as Icon;
                NativeMethods.DestroyIcon(shinfo.hIcon);
                return(icon);
            }

            return(null);
        }
Exemple #18
0
        private static ImageSource GetIcon(string path, ShellIconSize shellIconSize)
        {
            NativeMethods.SHFILEINFO shinfo = new NativeMethods.SHFILEINFO();
            NativeMethods.SHGetFileInfo(path, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | (shellIconSize == ShellIconSize.Small ? SHGFI_SMALLICON : SHGFI_LARGEICON));
            IntPtr iconHandle = shinfo.hIcon;

            if (IntPtr.Zero == iconHandle)
            {
                return(null);
            }
            ImageSource img = Imaging.CreateBitmapSourceFromHIcon(iconHandle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

            NativeMethods.DestroyIcon(iconHandle);
            return(img);
        }
Exemple #19
0
        int GetOsIcon(string path)
        {
            NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
            IntPtr sysImageList = NativeMethods.SHGetFileInfoW(path, 0, ref fileinfo,
                                                               (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_SHELLICONSIZE |
                                                               NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON);

            if (sysImageList == IntPtr.Zero)
            {
                return(-1);
            }

            ProjectIconReference handle = new ProjectIconReference(sysImageList, fileinfo.iIcon);

            return(ResolveReference(handle));
        }
Exemple #20
0
        public static Image GetSmallImage(SpecialFolder specialFolder)
        {
            uint dwflag = NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_ICON;
            int  dwAttr = 0;

            IntPtr pidl;

            NativeMethods.Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, (int)specialFolder, out pidl);

            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
            NativeMethods.Shell32.SHGetFileInfo(pidl, dwAttr, ref shfi, Marshal.SizeOf(shfi), dwflag);

            IntPtr iconPtr = NativeMethods.ComCtl32.ImageList_GetIcon(m_smallImageListHandle, shfi.iIcon, NativeMethods.ILD_NORMAL);

            return(Icon.FromHandle(iconPtr).ToBitmap());
        }
        protected Bitmap GetGenericIcon(IntPtr ptr, IconSize size, bool isFolder = false, bool forceLoad = false)
        {
            switch (size)
            {
            case IconSize.Thumbnail:
            case IconSize.ExtraLarge:
            case IconSize.Large:
            case IconSize.Jumbo:
                var retImage = GetBitmap(size, ptr, isFolder, forceLoad);
                return(ImageTools.ResizeImage(ImageTools.CheckImage(retImage) ? retImage : ImageTools.CutImage(retImage, new Size(48, 48)), IconSizeToSize(size), 0));
            }

            var shinfo = new NativeMethods.SHFILEINFO();

            var flags = NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_PIDL;

            if (!isFolder)
            {
                flags |= NativeMethods.SHGFI_USEFILEATTRIBUTES;
            }

            if (size == IconSize.Small)
            {
                flags = flags | NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON;
            }
            else
            {
                flags = flags | NativeMethods.SHGFI_ICON;
            }
            try
            {
                NativeMethods.SHGetFileInfo(ptr, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags);
            }
            catch
            {
                return(new Bitmap(1, 1));
            }

            if (shinfo.hIcon == IntPtr.Zero)
            {
                return(new Bitmap(1, 1));
            }
            var retVal = Icon.FromHandle(shinfo.hIcon).ToBitmap();

            NativeMethods.DestroyIcon(shinfo.hIcon);
            return(retVal);
        }
Exemple #22
0
        static string GetTypeNameForExtension(string extension)
        {
            Debug.Assert(!string.IsNullOrEmpty(extension));

            string dummyPath = Path.Combine(Path.GetTempPath(), "Dummy." + extension);

            NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
            IntPtr rslt = NativeMethods.SHGetFileInfoW(dummyPath, (uint)(FileAttributes.Normal), ref fileinfo,
                                                       (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_TYPENAME | NativeMethods.SHGFI_USEFILEATTRIBUTES);

            if (rslt == IntPtr.Zero)
            {
                return(null);
            }

            return(fileinfo.szTypeName);
        }
Exemple #23
0
        private int GetIconIndex(IntPtr pidlPtr, bool isDirectory, bool forceLoadFromDisk, ShellIconStateConstants iconState)
        {
            NativeMethods.SHGetFileInfoConstants dwFlags; uint dwAttr;
            GetAttributes(isDirectory, forceLoadFromDisk, out dwAttr, out dwFlags);
            dwFlags |= NativeMethods.SHGetFileInfoConstants.SHGFI_PIDL;

            var shfi     = new NativeMethods.SHFILEINFO();
            var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());
            var retVal   = NativeMethods.SHGetFileInfo(pidlPtr, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags) | (uint)iconState));

            if (!retVal.Equals(IntPtr.Zero))
            {
                return(shfi.iIcon.ToInt32());
            }
            Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index");
            return(-1);
        }
Exemple #24
0
        static ShellImageList()
        {
            IntPtr desktopFolderPtr;

            NativeMethods.Shell32.SHGetDesktopFolder(out desktopFolderPtr);
            m_desktopFolder = (NativeMethods.IShellFolder)Marshal.GetObjectForIUnknown(desktopFolderPtr);

            m_imageTable = new Hashtable();

            uint flag = NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON;

            NativeMethods.SHFILEINFO shfiSmall = new NativeMethods.SHFILEINFO();
            m_smallImageListHandle = NativeMethods.Shell32.SHGetFileInfo(".txt", NativeMethods.FILE_ATTRIBUTE_NORMAL, ref shfiSmall, Marshal.SizeOf(shfiSmall), flag);

            flag = NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_LARGEICON;
            NativeMethods.SHFILEINFO shfiLarge = new NativeMethods.SHFILEINFO();
            m_largeImageListHandle = NativeMethods.Shell32.SHGetFileInfo(".txt", NativeMethods.FILE_ATTRIBUTE_NORMAL, ref shfiLarge, Marshal.SizeOf(shfiLarge), flag);
        }
Exemple #25
0
        /// <summary>
        /// Uses SHGetFileInfo to retrieve the icon for the given file, folder or
        /// drive.
        /// </summary>
        /// <param name="info">The file system object to query the description of.</param>
        /// <returns>An Icon object containing the bitmap</returns>
        public static Icon GetIcon(this FileSystemInfo info)
        {
            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
            NativeMethods.SHGetFileInfo(info.FullName, 0, ref shfi, Marshal.SizeOf(shfi),
                                        NativeMethods.SHGetFileInfoFlags.SHGFI_SMALLICON |
                                        NativeMethods.SHGetFileInfoFlags.SHGFI_ICON);

            if (shfi.hIcon != IntPtr.Zero)
            {
                return(Icon.FromHandle(shfi.hIcon));
            }
            else
            {
                throw new IOException(string.Format(CultureInfo.CurrentCulture,
                                                    "Could not load file icon from {0}", info.FullName),
                                      Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()));
            }
        }
Exemple #26
0
        int GetSpecialIcon(string name, FileAttributes attr)
        {
            EnsureSpecialImages();

            NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
            IntPtr sysImageList = NativeMethods.SHGetFileInfoW(name, (uint)(int)attr, ref fileinfo,
                                                               (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_SHELLICONSIZE |
                                                               NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_USEFILEATTRIBUTES);

            if (sysImageList == IntPtr.Zero)
            {
                return(-1);
            }

            ProjectIconReference handle = new ProjectIconReference(sysImageList, fileinfo.iIcon);

            return(ResolveReference(handle));
        }
Exemple #27
0
        public int GetSpecialFolderIcon(WindowsSpecialFolder folder)
        {
            EnsureSpecialImages();

            int index;

            if (_folderMap.TryGetValue(folder, out index))
            {
                return(index);
            }

            IntPtr pidl = IntPtr.Zero;

            try
            {
                if (VSErr.S_OK != NativeMethods.SHGetFolderLocation(IntPtr.Zero, folder, IntPtr.Zero, 0, out pidl))
                {
                    return(-1);
                }


                NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
                IntPtr sysImageList = NativeMethods.SHGetFileInfoW(pidl, (uint)(int)FileAttributes.Directory, ref fileinfo,
                                                                   (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_SHELLICONSIZE |
                                                                   NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_PIDL);

                if (sysImageList == IntPtr.Zero)
                {
                    return(-1);
                }

                ProjectIconReference handle = new ProjectIconReference(sysImageList, fileinfo.iIcon);

                return(_folderMap[folder] = ResolveReference(handle));
            }
            finally
            {
                if (pidl != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pidl);
                }
            }
        }
        void appendList(ref ListView listView, string[] filenames)
        {
            // Obtain a handle to the system image list.
            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
            IntPtr hSysImgList            = NativeMethods.SHGetFileInfo("",
                                                                        0,
                                                                        ref shfi,
                                                                        (uint)Marshal.SizeOf(shfi),
                                                                        NativeMethods.SHGFI_SYSICONINDEX
                                                                        | NativeMethods.SHGFI_SMALLICON);

            Debug.Assert(hSysImgList != IntPtr.Zero);  // cross our fingers and hope to succeed!

            // Set the ListView control to use that image list.
            IntPtr hOldImgList = NativeMethods.SendMessage(listView.Handle,
                                                           NativeMethods.LVM_SETIMAGELIST,
                                                           NativeMethods.LVSIL_SMALL,
                                                           hSysImgList);

            // If the ListView control already had an image list, delete the old one.
            if (hOldImgList != IntPtr.Zero)
            {
                NativeMethods.ImageList_Destroy(hOldImgList);
            }

            NativeMethods.SetWindowTheme(listView.Handle, "Explorer", null);


            foreach (string file in filenames)
            {
                IntPtr himl = NativeMethods.SHGetFileInfo(file,
                                                          0,
                                                          ref shfi,
                                                          (uint)Marshal.SizeOf(shfi),
                                                          NativeMethods.SHGFI_DISPLAYNAME
                                                          | NativeMethods.SHGFI_SYSICONINDEX
                                                          | NativeMethods.SHGFI_SMALLICON);
                Debug.Assert(himl == hSysImgList); // should be the same imagelist as the one we set
                var listViewItem = listView.Items.Add(shfi.szDisplayName, shfi.iIcon);
                listViewItem.SubItems.Add(file);
            }
        }
        private BitmapSource CreateBitMapSrcFromAppIcon()
        {
            BitmapSource src;
            IntPtr       res = IntPtr.Zero;

            if (Environment.Is64BitProcess)
            {
                NativeMethods.SHFILEINFO info = new NativeMethods.SHFILEINFO();
                res = NativeMethods.SHGetFileInfo(obj.Proc.MainModule.FileName, 0, ref info, (uint)Marshal.SizeOf(info), 0x100 | 0x000 | 0x010);
                //dont forget to call DestroyIcon
                src = Imaging.CreateBitmapSourceFromHIcon(info.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            }
            else
            {
                src = BitmapSource.Create(1, 1, 1, 1, PixelFormats.BlackWhite, null, new byte[] { 0 }, 1);
            }

            NativeMethods.DestroyIcon(res);
            return(src);
        }
Exemple #30
0
            internal static void GetAttributes(IntPtr pidl, ref uint uintAttributes)
            {
                if (pidl == IntPtr.Zero)
                {
                    uintAttributes = 0;
                    return;
                }
                NativeMethods.SHFILEINFO sfi = new NativeMethods.SHFILEINFO(true);
                sfi.dwAttributes = uintAttributes;
                int    cbFileInfo = Marshal.SizeOf(sfi);
                uint   flags      = NativeMethods.SHGFI_ATTRIBUTES | NativeMethods.SHGFI_PIDL;
                IntPtr res        = NativeMethods.SHGetFileInfo(pidl, NativeMethods.FILE_ATTRIBUTE_NORMAL, out sfi, (uint)cbFileInfo, flags);

                if (res == IntPtr.Zero)
                {
                    uintAttributes = 0;
                    return;
                }
                uintAttributes &= sfi.dwAttributes;
            }
Exemple #31
0
        public static Icon ForFile(string name, IconSize size, bool linkOverlay)
        {
            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
            NativeMethods.FileInfoFlags flags = NativeMethods.FileInfoFlags.SHGFI_ICON | NativeMethods.FileInfoFlags.SHGFI_USEFILEATTRIBUTES;

            if (linkOverlay) flags |= NativeMethods.FileInfoFlags.SHGFI_LINKOVERLAY;

            flags |= (IconSize.Small == size) ? NativeMethods.FileInfoFlags.SHGFI_SMALLICON : NativeMethods.FileInfoFlags.SHGFI_LARGEICON;

            NativeMethods.SHGetFileInfo(name,
                NativeMethods.FILE_ATTRIBUTE_NORMAL,
                out shfi,
                (uint)Marshal.SizeOf(shfi),
                flags);

            // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
            Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
            NativeMethods.DestroyIcon(shfi.hIcon);		// Cleanup
            return icon;
        }
        /// <summary>
        /// get directory icon as byte array
        /// </summary>
        /// <param name="filePath">directory path</param>
        /// <returns>byte array</returns>
        public static byte[] GetDirectoryIcon(string filePath)
        {
            var    shinfo = new NativeMethods.SHFILEINFO();
            IntPtr hImg   = NativeMethods.SHGetFileInfo(
                filePath, 0, out shinfo, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(NativeMethods.SHFILEINFO)),
                (uint)(NativeMethods.SHGFI.SHGFI_ICON | NativeMethods.SHGFI.SHGFI_LARGEICON));

            if (IntPtr.Zero != hImg)
            {
                using (var icon = System.Drawing.Icon.FromHandle(shinfo.hIcon))
                    using (var stream = new MemoryStream()) {
                        var bmp = icon.ToBitmap();
                        bmp.Save(stream, ImageFormat.Png);
                        return(stream.GetBuffer());
                    }
            }
            else
            {
                return(null);
            }
        }
Exemple #33
0
        /// <summary>
        /// Retrieves general file information.
        /// </summary>
        /// <param name="pidl">Source object for which info should be retrieved.</param>
        /// <param name="displayName">Reference to variable where to store file display name.</param>
        /// <param name="type">Reference to variable where to store file type.</param>
        /// <param name="iconIndexSmall">Reference to variable where to store small icon index.</param>
        /// <param name="iconSmall">Reference to variable where to store small icon handle.</param>
        /// <param name="iconIndexLarge">Reference to variable where to store large icon index.</param>
        /// <param name="iconLarge">Reference to variable where to store large icon handle.</param>
        /// <returns>True if succeeded, false otherwise.</returns>
        public static bool RetrieveFileInfo(Pidl pidl, ref string displayName, ref string type, ref int iconIndexSmall, ref IntPtr iconSmall, ref int iconIndexLarge, ref IntPtr iconLarge)
        {
            NativeMethods.SHFILEINFO info = new NativeMethods.SHFILEINFO(true);
            int  cbFileInfo = Marshal.SizeOf(info);
            uint flags      = NativeMethods.SHGFI_PIDL |
                              NativeMethods.SHGFI_USEFILEATTRIBUTES |
                              NativeMethods.SHGFI_DISPLAYNAME |
                              NativeMethods.SHGFI_TYPENAME |
                              NativeMethods.SHGFI_SYSICONINDEX |
                              NativeMethods.SHGFI_ICON |
                              NativeMethods.SHGFI_SMALLICON;

            if (NativeMethods.SHGetFileInfo(pidl.Handle, 0, out info, (uint)cbFileInfo, flags) == IntPtr.Zero)
            {
                return(false);
            }

            displayName    = info.szDisplayName;
            type           = info.szTypeName;
            iconIndexSmall = info.iIcon;
            iconSmall      = info.hIcon;

            info.hIcon = IntPtr.Zero;
            flags      = NativeMethods.SHGFI_PIDL |
                         NativeMethods.SHGFI_USEFILEATTRIBUTES |
                         NativeMethods.SHGFI_SYSICONINDEX |
                         NativeMethods.SHGFI_ICON |
                         NativeMethods.SHGFI_LARGEICON;

            if (NativeMethods.SHGetFileInfo(pidl.Handle, 0, out info, (uint)cbFileInfo, flags) == IntPtr.Zero)
            {
                NativeMethods.DestroyIcon(info.hIcon);
                return(false);
            }

            iconIndexLarge = info.iIcon;
            iconLarge      = info.hIcon;
            return(true);
        }
Exemple #34
0
        private void InitializeObject()
        {
            NativeMethods.SHFILEINFO shfi =
            new NativeMethods.SHFILEINFO();

            // Attempt to get the information for the shell object.
            NativeMethods.SHGetFileInfo(
            m_pidl,
            0,
            ref shfi,
            Marshal.SizeOf(shfi),
            NativeMethods.SHGFI.SHGFI_PIDL |
            NativeMethods.SHGFI.SHGFI_DISPLAYNAME |
            NativeMethods.SHGFI.SHGFI_ATTRIBUTES |
            NativeMethods.SHGFI.SHGFI_TYPENAME
            );

            // Save the information.
            m_displayName = shfi.szDisplayName;
            m_typeName = shfi.szTypeName;
            m_attributes = (NativeMethods.SFGAO)shfi.dwAttributes;

            StringBuilder sb = new StringBuilder(260);

            // Get the physical path to the shell object.
            NativeMethods.SHGetPathFromIDList(
            m_pidl,
            sb
            );

            // Save the path.
            m_physicalPath = sb.ToString();
        }
Exemple #35
0
        public static Icon IconFromExtensionShell(string extension, IconSize size)
        {
            //add '.' if nessesry
            if (extension[0] != '.') extension = '.' + extension;

            //temp struct for getting file shell info
            NativeMethods.SHFILEINFO fileInfo = new NativeMethods.SHFILEINFO();

            NativeMethods.SHGetFileInfo(
                extension,
                0,
                out fileInfo,
                Convert.ToUInt32(Marshal.SizeOf(fileInfo)),
                NativeMethods.FileInfoFlags.SHGFI_ICON | NativeMethods.FileInfoFlags.SHGFI_USEFILEATTRIBUTES |
                (size == IconSize.Large ? NativeMethods.FileInfoFlags.SHGFI_LARGEICON : NativeMethods.FileInfoFlags.SHGFI_SMALLICON)
            );

            return Icon.FromHandle(fileInfo.hIcon);
        }
Exemple #36
0
        public static Icon ForGenericFolder(IconSize size, FolderType folderType)
        {
            // Need to add size check, although errors generated at present!
            NativeMethods.FileInfoFlags flags = NativeMethods.FileInfoFlags.SHGFI_ICON | NativeMethods.FileInfoFlags.SHGFI_USEFILEATTRIBUTES;

            if (FolderType.Open == folderType)
                flags |= NativeMethods.FileInfoFlags.SHGFI_OPENICON;

            flags |= (IconSize.Small == size) ? NativeMethods.FileInfoFlags.SHGFI_SMALLICON : NativeMethods.FileInfoFlags.SHGFI_LARGEICON;

            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO();
            NativeMethods.SHGetFileInfo(
                null,
                NativeMethods.FILE_ATTRIBUTE_DIRECTORY,
                out shfi,
                (uint)Marshal.SizeOf(shfi),
                flags
                );

            Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
            NativeMethods.DestroyIcon(shfi.hIcon);		// Cleanup
            return icon;
        }
Exemple #37
0
        int GetOsIcon(string path)
        {
            NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
            IntPtr sysImageList = NativeMethods.SHGetFileInfoW(path, 0, ref fileinfo,
                (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_SHELLICONSIZE |
                NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON);

            if (sysImageList == IntPtr.Zero)
                return -1;

            ProjectIconReference handle = new ProjectIconReference(sysImageList, fileinfo.iIcon);

            return ResolveReference(handle);
        }
Exemple #38
0
        int GetSpecialIcon(string name, FileAttributes attr)
        {
            EnsureSpecialImages();

            NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
            IntPtr sysImageList = NativeMethods.SHGetFileInfoW(name, (uint)(int)attr, ref fileinfo,
                (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_SHELLICONSIZE |
                NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_USEFILEATTRIBUTES);

            if (sysImageList == IntPtr.Zero)
                return -1;

            ProjectIconReference handle = new ProjectIconReference(sysImageList, fileinfo.iIcon);

            return ResolveReference(handle);
        }
Exemple #39
0
        static string GetTypeName(string path)
        {
            NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
            IntPtr rslt = NativeMethods.SHGetFileInfoW(path, 0, ref fileinfo,
                (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_TYPENAME);

            if (rslt == IntPtr.Zero)
                return null;

            return fileinfo.szTypeName;
        }
Exemple #40
0
        static string GetTypeNameForExtension(string extension)
        {
            Debug.Assert(!string.IsNullOrEmpty(extension));

            string dummyPath = Path.Combine(Path.GetTempPath(), "Dummy." + extension);

            NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
            IntPtr rslt = NativeMethods.SHGetFileInfoW(dummyPath, (uint)(FileAttributes.Normal), ref fileinfo,
                (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_TYPENAME | NativeMethods.SHGFI_USEFILEATTRIBUTES);

            if (rslt == IntPtr.Zero)
                return null;

            return fileinfo.szTypeName;
        }
        private int GetIconIndex(IntPtr pidlPtr, bool isDirectory, bool forceLoadFromDisk, ShellIconStateConstants iconState)
        {
            NativeMethods.SHGetFileInfoConstants dwFlags; uint dwAttr;
            GetAttributes(isDirectory, forceLoadFromDisk, out dwAttr, out dwFlags);
            dwFlags |= NativeMethods.SHGetFileInfoConstants.SHGFI_PIDL;

            var shfi = new NativeMethods.SHFILEINFO();
            var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());
            var retVal = NativeMethods.SHGetFileInfo(pidlPtr, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags) | (uint)iconState));

            if (!retVal.Equals(IntPtr.Zero)) return shfi.iIcon.ToInt32();
            Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index");
            return -1;
        }
Exemple #42
0
        public int GetSpecialFolderIcon(WindowsSpecialFolder folder)
        {
            EnsureSpecialImages();

            int index;

            if (_folderMap.TryGetValue(folder, out index))
                return index;

            IntPtr pidl = IntPtr.Zero;
            try
            {
                if (VSConstants.S_OK != NativeMethods.SHGetFolderLocation(IntPtr.Zero, folder, IntPtr.Zero, 0, out pidl))
                    return -1;

                NativeMethods.SHFILEINFO fileinfo = new NativeMethods.SHFILEINFO();
                IntPtr sysImageList = NativeMethods.SHGetFileInfoW(pidl, (uint)(int)FileAttributes.Directory, ref fileinfo,
                                                            (uint)Marshal.SizeOf(fileinfo), NativeMethods.SHGFI_SHELLICONSIZE |
                                                            NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_PIDL);

                if (sysImageList == IntPtr.Zero)
                    return -1;

                ProjectIconReference handle = new ProjectIconReference(sysImageList, fileinfo.iIcon);

                return _folderMap[folder] = ResolveReference(handle);
            }
            finally
            {
                if (pidl != IntPtr.Zero)
                    Marshal.FreeCoTaskMem(pidl);
            }
        }
        private int GetIconIndex(string fileName, bool isDirectory, bool forceLoadFromDisk, ShellIconStateConstants iconState)
        {
            NativeMethods.SHGetFileInfoConstants dwFlags; uint dwAttr;
            GetAttributes(isDirectory, forceLoadFromDisk, out dwAttr, out dwFlags);

            // sFileSpec can be any file.

            if (fileName.EndsWith(".lnk", StringComparison.InvariantCultureIgnoreCase))
            {
                dwFlags |= NativeMethods.SHGetFileInfoConstants.SHGFI_LINKOVERLAY | NativeMethods.SHGetFileInfoConstants.SHGFI_ICON;
                iconState = ShellIconStateConstants.ShellIconStateLinkOverlay;
                forceLoadFromDisk = true;
            }

            var shfi = new NativeMethods.SHFILEINFO();
            var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());
            var retVal = NativeMethods.SHGetFileInfo(fileName, dwAttr, ref shfi, shfiSize, ((uint)(dwFlags) | (uint)iconState));

            if (!retVal.Equals(IntPtr.Zero)) return shfi.iIcon.ToInt32();
            if (forceLoadFromDisk)
                return GetIconIndex(Path.GetFileName(fileName), isDirectory, false, iconState);
            Debug.Assert((!retVal.Equals(IntPtr.Zero)), "Failed to get icon index");
            return -1;
        }
 public static IntPtr Test()
 {
     var shfi = new NativeMethods.SHFILEINFO();
     var shfiSize = (uint)Marshal.SizeOf(shfi.GetType());
     return NativeMethods.SHGetFileInfo(@"C:\", 16, ref shfi, shfiSize, 16384);
 }
Exemple #45
0
        public static void GetWindowSmallIconAsBitmap(IntPtr hWnd, Action<Bitmap> action)
        {
            IntPtr result;
            NativeMethods.SendMessageTimeout(hWnd, NativeMethods.WM_GETICON, NativeMethods.ICON_SMALL,
                IntPtr.Zero, NativeMethods.SMTO.SMTO_BLOCK, 500, out result);

            if (result == IntPtr.Zero)
            {
                NativeMethods.SendMessageTimeout(hWnd, NativeMethods.WM_QUERYDRAGICON, UIntPtr.Zero,
                    IntPtr.Zero, NativeMethods.SMTO.SMTO_BLOCK, 500, out result);
            }

            if (result == IntPtr.Zero)
            {
                result = NativeMethods.GetClassLongPtr(hWnd, NativeMethods.GCL_HICONSM);
            }

            if (result == IntPtr.Zero)
            {
                Task.Factory.StartNew(hWnd2 =>
                    {
                        Bitmap bitmap = null;
                        try
                        {
                            int processId;
                            NativeMethods.GetWindowThreadProcessId((IntPtr) hWnd2, out processId);
                            var processFileName = Process.GetProcessById(processId).MainModule.FileName;

                            var info = new NativeMethods.SHFILEINFO();

                            NativeMethods.SHGetFileInfo(processFileName, 0, ref info,
                                Marshal.SizeOf(info), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON);

                            if (info.hIcon != IntPtr.Zero)
                            {
                                bitmap = new Bitmap(Bitmap.FromHicon(info.hIcon), SystemAndProcessInformation.smallIconSize);
                                NativeMethods.DestroyIcon(info.hIcon);
                            }
                            else
                            {
                                var icon = Icon.ExtractAssociatedIcon(processFileName);
                                if (icon != null)
                                {
                                    bitmap = new Bitmap(icon.ToBitmap(), SystemAndProcessInformation.smallIconSize);
                                }
                            }
                        }
                        catch
                        {
                        }

                        return bitmap;
                    }, hWnd).ContinueWith(t => action(t.Result), TaskScheduler.FromCurrentSynchronizationContext());
            }
            else
            {
                Bitmap bitmap = null;
                try
                {
                    bitmap = new Bitmap(Bitmap.FromHicon(result), SystemAndProcessInformation.smallIconSize);
                }
                catch
                {
                }
                action(bitmap);
            }
        }
        protected static Bitmap GetGenericIcon(string fullPathOrExt, IconSize size, bool isFolder = false, bool forceLoad = false)
        {
            try
            {
                var fileName = fullPathOrExt.StartsWith(".") ? "AAA" + fullPathOrExt : fullPathOrExt;

                switch (size)
                {
                    case IconSize.Thumbnail:
                    case IconSize.ExtraLarge:
                    case IconSize.Jumbo:
                        Bitmap retImage = null;

                        try
                        {
                            retImage = GetBitmap(size, fileName, isFolder, forceLoad);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("GetGenericIcon - " + ex.Message);
                            //TO-DO: Fix exception:
                            //GetGenericIcon Unable to cast COM object of type 'System.__ComObject' to interface type 'IImageList'. 
                            //This operation failed because the QueryInterface call on the COM component for the interface with IID 
                            //'{46EB5926-582E-4017-9FDF-E8998DAA0950}' failed due to the following error: No such interface supported 
                            //(Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

                            //FailSafe
                            if (size > IconSize.Large)
                                return GetGenericIcon(fullPathOrExt, IconSize.Large, isFolder, forceLoad);
                        }

                        return ImageTools.ResizeImage(ImageTools.CheckImage(retImage) ? retImage : ImageTools.CutImage(retImage, new Size(48, 48)), IconSizeToSize(size), 0);
                }

                var shinfo = new NativeMethods.SHFILEINFO();

                var flags = NativeMethods.SHGFI_SYSICONINDEX;
                if (!isFolder)
                    flags |= NativeMethods.SHGFI_USEFILEATTRIBUTES;

                if (size == IconSize.Small)
                    flags = flags | NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON;
                else flags = flags | NativeMethods.SHGFI_ICON;
                try
                {
                    NativeMethods.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("GetGenericIcon - " + ex.Message);
                    return new Bitmap(1, 1);
                }
                if (shinfo.hIcon == IntPtr.Zero) return new Bitmap(1, 1);
                var retVal = Icon.FromHandle(shinfo.hIcon).ToBitmap();
                NativeMethods.DestroyIcon(shinfo.hIcon);
                return retVal;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("GetGenericIcon - " + ex.Message);
                return new Bitmap(1, 1);
            }
        }
        protected Bitmap GetGenericIcon(IntPtr ptr, IconSize size, bool isFolder = false, bool forceLoad = false)
        {
            switch (size)
            {
                case IconSize.Thumbnail:
                case IconSize.ExtraLarge:
                case IconSize.Large:
                case IconSize.Jumbo:
                    var retImage = GetBitmap(size, ptr, isFolder, forceLoad);
                    return ImageTools.ResizeImage(ImageTools.CheckImage(retImage) ? retImage : ImageTools.CutImage(retImage, new Size(48, 48)), IconSizeToSize(size), 0);
            }

            var shinfo = new NativeMethods.SHFILEINFO();

            var flags = NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_PIDL;
            if (!isFolder)
                flags |= NativeMethods.SHGFI_USEFILEATTRIBUTES;

            if (size == IconSize.Small)
                flags = flags | NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON;
            else flags = flags | NativeMethods.SHGFI_ICON;
            try
            {
                NativeMethods.SHGetFileInfo(ptr, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags);
            }
            catch
            {
                return new Bitmap(1, 1);
            }

            if (shinfo.hIcon == IntPtr.Zero) return new Bitmap(1, 1);
            var retVal = Icon.FromHandle(shinfo.hIcon).ToBitmap();
            NativeMethods.DestroyIcon(shinfo.hIcon);
            return retVal;
        }