Exemplo n.º 1
0
        private static uint GetFileInfoRequestFlags(Win32.ItemState itemState, Win32.IconSize iconSize, Win32.FileAttribute attributes)
        {
            var flags = (uint)(Win32.ShellAttribute.Icon | Win32.ShellAttribute.UseFileAttributes);

            if (attributes == Win32.FileAttribute.Directory && itemState == Win32.ItemState.Open)
            {
                flags |= (uint)Win32.ShellAttribute.OpenIcon;
            }
            flags |= iconSize == Win32.IconSize.Small ? (uint)Win32.ShellAttribute.SmallIcon : (uint)Win32.ShellAttribute.LargeIcon;
            return(flags);
        }
Exemplo n.º 2
0
 public static ImageSource GetImageSource(string fileSystemItemPath, Win32.ItemState itemState = Win32.ItemState.Undefined, Win32.IconSize iconSize = Win32.IconSize.Small)
 {
     using var fileIcon = GetIcon(fileSystemItemPath, itemState, iconSize);
     return(IconToImageSource(fileIcon) ?? IconToImageSource(EmptyIcon()));
 }
Exemplo n.º 3
0
        public static Icon GetIcon(string fileSystemItemPath, Win32.ItemState itemState = Win32.ItemState.Undefined, Win32.IconSize iconSize = Win32.IconSize.Small)
        {
            try
            {
                var attributes = GetFileInfoRequestAttributes(fileSystemItemPath);
                var flags      = GetFileInfoRequestFlags(itemState, iconSize, attributes);
                var fileInfo   = new Win32.ShellFileInfo();
                var size       = (uint)Marshal.SizeOf(fileInfo);

                var result = Win32.SHGetFileInfo(fileSystemItemPath, (uint)attributes, out fileInfo, size, flags);
                return(result.IsValidHandle()
                    ? GetIconFromHandle(fileInfo.hIcon)
                    : EmptyIcon());
            }
            catch
            {
                return(EmptyIcon());
            }
        }