Exemplo n.º 1
0
        /// <summary>
        ///     Gets an icon from a file path using windows shell
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns>ImageSource for the icon for the icon at filePath</returns>
        public static ImageSource IconFromFilePath(string filePath, IconSize size, bool shouldFallBack = true)
        {
            MemoryCache memoryCache;

            switch (size)
            {
            case IconSize.Large:
                memoryCache = LargeIconCache;
                break;

            case IconSize.Small:
                memoryCache = SmallIconCache;
                break;

            default:
                throw new ArgumentOutOfRangeException($"{nameof(size)} is not a valid icon size");
            }
            try
            {
                // todo: IconCache.CacheMemoryLimit; setting
                var cacheItem = memoryCache.GetCacheItem(filePath);
                if (cacheItem != null)
                {
                    return(cacheItem.Value as ImageSource);
                }
                var shinfo    = new NativeMethods.ShFileInfo();
                var hImgSmall = NativeMethods.SHGetFileInfo(filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
                                                            NativeMethods.SHGFI_ICON | size.ToIconFlag());
                var icon = Icon.FromHandle(shinfo.hIcon);
                var bmp  = icon.ToBitmap();
                var img  = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
                                                                 BitmapSizeOptions.FromEmptyOptions());
                img.Freeze();
                var newItem = new CacheItem(filePath, img);
                var policy  = new CacheItemPolicy
                {
                    SlidingExpiration = TimeSpan.FromMinutes(10)
                };
                memoryCache.Add(newItem, policy);
                return(img);
            }
            catch (Exception)
            {
                if (!shouldFallBack)
                {
                    return(null);
                }
                switch (size)
                {
                case IconSize.Large:
                    return(UnknownFileLarge);

                case IconSize.Small:
                    return(UnknownFileSmall);

                default:
                    throw new ArgumentOutOfRangeException($"{nameof(size)} is not a valid IconSize");
                }
            }
        }
Exemplo n.º 2
0
        public static ShellFileInfo GetShellFileInfo(string filePath)
        {
            var shinfo    = new NativeMethods.ShFileInfo();
            var hImgSmall = NativeMethods.SHGetFileInfo(filePath, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
                                                        NativeMethods.SHGFI_ICON | IconSize.Small.ToIconFlag());
            var icon = Icon.FromHandle(shinfo.hIcon);
            var bmp  = icon.ToBitmap();
            var img  = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
                                                             BitmapSizeOptions.FromEmptyOptions());

            img.Freeze();
            var    fvi         = FileVersionInfo.GetVersionInfo(filePath);
            string displayName = Path.GetFileName(filePath);

            if (!string.IsNullOrWhiteSpace(fvi.ProductName))
            {
                displayName = fvi.ProductName;
            }
            return(new ShellFileInfo
            {
                Icon = img,
                DisplayName = displayName,
                Description = fvi.FileDescription,
                FilePath = filePath
            });
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Initializes the <see cref="ShellHelper" /> class.
        /// </summary>
        static ShellHelper()
        {
            var shinfo    = new NativeMethods.ShFileInfo();
            var hImgSmall = NativeMethods.SHGetFileInfo("unknownfile32x32.ico", 0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
                                                        NativeMethods.SHGFI_ICON | IconSize.Large.ToIconFlag());
            var icon = Icon.FromHandle(shinfo.hIcon);
            var bmp  = icon.ToBitmap();
            var img  = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
                                                             BitmapSizeOptions.FromEmptyOptions());

            img.Freeze();
            UnknownFileLarge = img;

            hImgSmall = NativeMethods.SHGetFileInfo("unknownfile16x16.ico", 0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
                                                    NativeMethods.SHGFI_ICON | IconSize.Small.ToIconFlag());
            icon = Icon.FromHandle(shinfo.hIcon);
            bmp  = icon.ToBitmap();
            img  = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
                                                         BitmapSizeOptions.FromEmptyOptions());
            img.Freeze();
            UnknownFileSmall = img;
        }