forked from adyanth/QuickLook.Plugin.FolderViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemIconProvider.cs
More file actions
121 lines (108 loc) · 4.06 KB
/
Copy pathSystemIconProvider.cs
File metadata and controls
121 lines (108 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace QuickLook.Plugin.FolderViewer
{
internal static class SystemIconProvider
{
private const int MaximumCachedIcons = 512;
private const int MaximumExtensionLength = 24;
private const uint FileAttributeDirectory = 0x10;
private const uint FileAttributeNormal = 0x80;
private const uint ShgfiIcon = 0x000000100;
private const uint ShgfiSmallIcon = 0x000000001;
private const uint ShgfiUseFileAttributes = 0x000000010;
private static readonly Dictionary<string, ImageSource> Cache =
new Dictionary<string, ImageSource>(StringComparer.OrdinalIgnoreCase);
private static readonly object CacheLock = new object();
public static ImageSource GetIcon(string fullPath, bool isFolder)
{
var key = isFolder ? "<folder>" : GetExtensionKey(fullPath);
lock (CacheLock)
{
if (Cache.TryGetValue(key, out var cached))
return cached;
if (Cache.Count >= MaximumCachedIcons)
key = isFolder ? "<folder>" : "<file>";
if (Cache.TryGetValue(key, out cached))
return cached;
var icon = LoadIcon(key, isFolder);
Cache[key] = icon;
return icon;
}
}
private static string GetExtensionKey(string path)
{
try
{
var extension = Path.GetExtension(path);
return string.IsNullOrEmpty(extension) || extension.Length > MaximumExtensionLength
? "<file>"
: extension;
}
catch (ArgumentException)
{
return "<file>";
}
}
private static ImageSource LoadIcon(string key, bool isFolder)
{
var attributes = isFolder ? FileAttributeDirectory : FileAttributeNormal;
var lookupName = isFolder ? "folder" : "file" + (key.StartsWith("<", StringComparison.Ordinal) ? string.Empty : key);
SHFILEINFO info;
var result = SHGetFileInfo(
lookupName,
attributes,
out info,
(uint)Marshal.SizeOf(typeof(SHFILEINFO)),
ShgfiIcon | ShgfiSmallIcon | ShgfiUseFileAttributes);
if (result == IntPtr.Zero || info.hIcon == IntPtr.Zero)
return null;
try
{
var source = Imaging.CreateBitmapSourceFromHIcon(
info.hIcon,
Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(16, 16));
source.Freeze();
return source;
}
catch (Exception exception) when (
exception is ArgumentException ||
exception is ExternalException)
{
return null;
}
finally
{
DestroyIcon(info.hIcon);
}
}
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbFileInfo,
uint uFlags);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DestroyIcon(IntPtr hIcon);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
}
}