-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellIconService.cs
More file actions
135 lines (112 loc) · 3.9 KB
/
Copy pathShellIconService.cs
File metadata and controls
135 lines (112 loc) · 3.9 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
namespace c2flux
{
public enum ShellStockIconId : uint
{
FolderOpen = 4,
Find = 22
}
public sealed class ShellIconService
{
private const int SHGFI_ICON = 0x100;
private const int SHGFI_SMALLICON = 0x1;
private const int SHGFI_USEFILEATTRIBUTES = 0x10;
private const uint FILE_ATTRIBUTE_DIRECTORY = 0x10;
private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
private const uint SHGSI_ICON = 0x000000100;
private const uint SHGSI_SMALLICON = 0x000000001;
[DllImport("shell32.dll")]
private static extern IntPtr SHGetFileInfo(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbFileInfo,
uint uFlags);
[DllImport("shell32.dll", SetLastError = false)]
private static extern int SHGetStockIconInfo(
ShellStockIconId siid,
uint uFlags,
ref SHSTOCKICONINFO psii);
[DllImport("user32.dll")]
private static extern bool DestroyIcon(IntPtr hIcon);
public Bitmap GetSmallSystemIcon(string path)
{
SHFILEINFO shellFileInfo = new SHFILEINFO();
bool directoryExists = Directory.Exists(path);
bool fileExists = File.Exists(path);
uint attributes = directoryExists
? FILE_ATTRIBUTE_DIRECTORY
: FILE_ATTRIBUTE_NORMAL;
uint flags = SHGFI_ICON | SHGFI_SMALLICON;
if (!directoryExists && !fileExists)
{
flags |= SHGFI_USEFILEATTRIBUTES;
}
IntPtr result = SHGetFileInfo(
path,
attributes,
ref shellFileInfo,
(uint)Marshal.SizeOf(typeof(SHFILEINFO)),
flags);
if (result == IntPtr.Zero || shellFileInfo.hIcon == IntPtr.Zero)
{
return SystemIcons.Application.ToBitmap();
}
try
{
using Icon icon = (Icon)Icon.FromHandle(shellFileInfo.hIcon).Clone();
return icon.ToBitmap();
}
finally
{
DestroyIcon(shellFileInfo.hIcon);
}
}
public Bitmap GetSmallStockIcon(ShellStockIconId stockIconId)
{
SHSTOCKICONINFO stockIconInfo = new SHSTOCKICONINFO();
stockIconInfo.cbSize = (uint)Marshal.SizeOf(typeof(SHSTOCKICONINFO));
int result = SHGetStockIconInfo(
stockIconId,
SHGSI_ICON | SHGSI_SMALLICON,
ref stockIconInfo);
if (result != 0 || stockIconInfo.hIcon == IntPtr.Zero)
{
return SystemIcons.Application.ToBitmap();
}
try
{
using Icon icon = (Icon)Icon.FromHandle(stockIconInfo.hIcon).Clone();
return icon.ToBitmap();
}
finally
{
DestroyIcon(stockIconInfo.hIcon);
}
}
[StructLayout(LayoutKind.Sequential)]
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;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct SHSTOCKICONINFO
{
public uint cbSize;
public IntPtr hIcon;
public int iSysImageIndex;
public int iIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szPath;
}
}
}