c#取文件图标问题 我想根据文件名(含扩展名)取得文件的图标该怎么做? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我记得是用API的,你google找下,有这方面的源码的,以前做过一个。 想把取出的图标赋给一个可以显示图片的控件,假设AAAAAA.value = ..........; 1namespace MaLio.IO.Icon {23 /// 4 /// reads icons from the windows shell5 /// 6 public class IconReader {78 /// 9 /// used to get/return information about a file10 /// in SHGetFileInfo call 11 /// 12 [System.FlagsAttribute()]13 private enum EnumFileInfoFlags : uint {14 /// get large icon15 LARGEICON = 0x000000000, 16 /// get small icon17 SMALLICON = 0x000000001, 18 /// get open icon19 OPENICON = 0x000000002, 20 /// get shell size icon21 SHELLICONSIZE = 0x000000004, 22 /// pszPath is a pidl23 PIDL = 0x000000008, 24 /// use passed dwFileAttribute25 USEFILEATTRIBUTES = 0x000000010, 26 /// apply the appropriate overlays27 ADDOVERLAYS = 0x000000020, 28 /// get the index of the overlay29 OVERLAYINDEX = 0x000000040, 30 /// get icon31 ICON = 0x000000100, 32 /// get display name33 DISPLAYNAME = 0x000000200, 34 /// get type name35 TYPENAME = 0x000000400, 36 /// get attributes37 ATTRIBUTES = 0x000000800, 38 /// get icon location39 ICONLOCATION = 0x000001000, 40 /// return exe type41 EXETYPE = 0x000002000, 42 /// get system icon index43 SYSICONINDEX = 0x000004000, 44 /// put a link overlay on icon45 LINKOVERLAY = 0x000008000, 46 /// show icon in selected state47 SELECTED = 0x000010000, 48 /// get only specified attributes49 ATTR_SPECIFIED = 0x000020000 50 }5152 /// 53 /// maxumum length of path 54 /// 55 private const int conMAX_PATH = 260;5657 /// 58 /// looking for folder59 /// 60 private const uint conFILE_ATTRIBUTE_DIRECTORY = 0x00000010; 6162 /// 63 /// looking for file64 /// 65 private const uint conFILE_ATTRIBUTE_NORMAL = 0x00000080; 6667 /// 68 /// size of the icon69 /// 70 public enum EnumIconSize {71 /// 32x3272 Large = 0,73 /// 16x1674 Small = 175 }76 77 /// 78 /// state of the folder79 /// 80 public enum EnumFolderType {81 /// open folder82 Open = 0,83 /// closed folder84 Closed = 185 }8687 /// 88 /// hold file/icon information89 /// see platformSDK SHFILEINFO90 /// 91 /// 92 /// be sure to call DestroyIcon [hIcon] when done93 /// 94 [System.Runtime.InteropServices.StructLayout(95 System.Runtime.InteropServices.LayoutKind.Sequential)]96 private struct ShellFileInfo { 9798 public const int conNameSize = 80;99 public System.IntPtr hIcon; // note to call DestroyIcon100 public int iIndex; 101 public uint dwAttributes; 102103 [System.Runtime.InteropServices.MarshalAs(104 System.Runtime.InteropServices.UnmanagedType.ByValTStr, 105 SizeConst=conMAX_PATH)]106 public string szDisplayName; 107108 [System.Runtime.InteropServices.MarshalAs(109 System.Runtime.InteropServices.UnmanagedType.ByValTStr, 110 SizeConst=conNameSize)]111 public string szTypeName; 112 };113114 /// 115 /// used to free a windows icon handle116 /// 117 /// "hIcon">icon handle.118 [System.Runtime.InteropServices.DllImport("User32.dll")]119 private static extern int DestroyIcon(System.IntPtr hIcon);120121 /// 122 /// gets file information 123 /// see platformSDK124 /// 125 [System.Runtime.InteropServices.DllImport("Shell32.dll")]126 private static extern System.IntPtr SHGetFileInfo(127 string pszPath,128 uint dwFileAttributes,129 ref ShellFileInfo psfi,130 uint cbFileInfo,131 uint uFlags132 );133134 /// 135 /// lookup and return an icon from windows shell136 /// 137 /// "name">path to the file138 /// "size">large or small139 /// "linkOverlay">true to include the overlay link iconlet140 /// requested icon141 public static System.Drawing.Icon GetFileIcon(142 string filePath, 143 EnumIconSize size, 144 bool addLinkOverlay) 145 {146147 EnumFileInfoFlags flags = 148 EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES;149150 // add link overlay if requested151 if (addLinkOverlay) {152 flags |= EnumFileInfoFlags.LINKOVERLAY;153 }154155 // set size156 if (size == EnumIconSize.Small) {157 flags |= EnumFileInfoFlags.SMALLICON;158 }159 else {160 flags |= EnumFileInfoFlags.LARGEICON;161 }162163 ShellFileInfo shellFileInfo = new ShellFileInfo();164165 SHGetFileInfo(166 filePath, 167 conFILE_ATTRIBUTE_NORMAL, 168 ref shellFileInfo, 169 (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), 170 (uint)flags);171172 // deep copy 173 System.Drawing.Icon icon = 174 (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone();175176 // release handle 177 DestroyIcon(shellFileInfo.hIcon); 178179 return icon;180 }181182 /// 183 /// lookup and return an icon from windows shell184 /// 185 /// "size">large or small186 /// "folderType">open or closed187 /// requested icon188 public static System.Drawing.Icon GetFolderIcon(189 EnumIconSize size, 190 EnumFolderType folderType) 191 {192 return GetFolderIcon(null, size, folderType);193 }194195 /// 196 /// lookup and return an icon from windows shell197 /// 198 /// "folderPath">path to folder 199 /// "size">large or small200 /// "folderType">open or closed201 /// requested icon202 public static System.Drawing.Icon GetFolderIcon(203 string folderPath, 204 EnumIconSize size, 205 EnumFolderType folderType) 206 {207 208 EnumFileInfoFlags flags = 209 EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES;210211 if (folderType == EnumFolderType.Open) {212 flags |= EnumFileInfoFlags.OPENICON;213 }214 215 if (EnumIconSize.Small == size) {216 flags |= EnumFileInfoFlags.SMALLICON;217 } 218 else {219 flags |= EnumFileInfoFlags.LARGEICON;220 }221 222 ShellFileInfo shellFileInfo = new ShellFileInfo();223 SHGetFileInfo(224 folderPath, 225 conFILE_ATTRIBUTE_DIRECTORY, 226 ref shellFileInfo, 227 (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo), 228 (uint)flags ); 229230 // deep copy 231 System.Drawing.Icon icon = 232 (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone();233234 // release handle235 DestroyIcon(shellFileInfo.hIcon); 236237 return icon;238 } 239 }240} 字符串组合的问题 如何连续清空 Dictionary<k,v>怎么绑定ListView 求教 设置一个文件夹的透明度 如何将多个参数传递到一个指定的URL上? 读取上传文件的问题?顺便散分! 以下的代码在使用时安全吗? 提交个问题,希望得到各位版主的帮助在线等。。。。。。。。。。 新人求助 数据修改问题 如何使用C#语言作为脚本? C#怎么实现按月循环 初学VS2008+sql2005者的愚蠢问题,请高手指教
AAA.value = ..........;
1namespace MaLio.IO.Icon {
2
3 ///
4 /// reads icons from the windows shell
5 ///
6 public class IconReader {
7
8 ///
9 /// used to get/return information about a file
10 /// in SHGetFileInfo call
11 ///
12 [System.FlagsAttribute()]
13 private enum EnumFileInfoFlags : uint {
14 /// get large icon
15 LARGEICON = 0x000000000,
16 /// get small icon
17 SMALLICON = 0x000000001,
18 /// get open icon
19 OPENICON = 0x000000002,
20 /// get shell size icon
21 SHELLICONSIZE = 0x000000004,
22 /// pszPath is a pidl
23 PIDL = 0x000000008,
24 /// use passed dwFileAttribute
25 USEFILEATTRIBUTES = 0x000000010,
26 /// apply the appropriate overlays
27 ADDOVERLAYS = 0x000000020,
28 /// get the index of the overlay
29 OVERLAYINDEX = 0x000000040,
30 /// get icon
31 ICON = 0x000000100,
32 /// get display name
33 DISPLAYNAME = 0x000000200,
34 /// get type name
35 TYPENAME = 0x000000400,
36 /// get attributes
37 ATTRIBUTES = 0x000000800,
38 /// get icon location
39 ICONLOCATION = 0x000001000,
40 /// return exe type
41 EXETYPE = 0x000002000,
42 /// get system icon index
43 SYSICONINDEX = 0x000004000,
44 /// put a link overlay on icon
45 LINKOVERLAY = 0x000008000,
46 /// show icon in selected state
47 SELECTED = 0x000010000,
48 /// get only specified attributes
49 ATTR_SPECIFIED = 0x000020000
50 }
51
52 ///
53 /// maxumum length of path
54 ///
55 private const int conMAX_PATH = 260;
56
57 ///
58 /// looking for folder
59 ///
60 private const uint conFILE_ATTRIBUTE_DIRECTORY = 0x00000010;
61
62 ///
63 /// looking for file
64 ///
65 private const uint conFILE_ATTRIBUTE_NORMAL = 0x00000080;
66
67 ///
68 /// size of the icon
69 ///
70 public enum EnumIconSize {
71 /// 32x32
72 Large = 0,
73 /// 16x16
74 Small = 1
75 }
76
77 ///
78 /// state of the folder
79 ///
80 public enum EnumFolderType {
81 /// open folder
82 Open = 0,
83 /// closed folder
84 Closed = 1
85 }
86
87 ///
88 /// hold file/icon information
89 /// see platformSDK SHFILEINFO
90 ///
91 ///
92 /// be sure to call DestroyIcon [hIcon] when done
93 ///
94 [System.Runtime.InteropServices.StructLayout(
95 System.Runtime.InteropServices.LayoutKind.Sequential)]
96 private struct ShellFileInfo {
97
98 public const int conNameSize = 80;
99 public System.IntPtr hIcon; // note to call DestroyIcon
100 public int iIndex;
101 public uint dwAttributes;
102
103 [System.Runtime.InteropServices.MarshalAs(
104 System.Runtime.InteropServices.UnmanagedType.ByValTStr,
105 SizeConst=conMAX_PATH)]
106 public string szDisplayName;
107
108 [System.Runtime.InteropServices.MarshalAs(
109 System.Runtime.InteropServices.UnmanagedType.ByValTStr,
110 SizeConst=conNameSize)]
111 public string szTypeName;
112 };
113
114 ///
115 /// used to free a windows icon handle
116 ///
117 /// "hIcon">icon handle.
118 [System.Runtime.InteropServices.DllImport("User32.dll")]
119 private static extern int DestroyIcon(System.IntPtr hIcon);
120
121 ///
122 /// gets file information
123 /// see platformSDK
124 ///
125 [System.Runtime.InteropServices.DllImport("Shell32.dll")]
126 private static extern System.IntPtr SHGetFileInfo(
127 string pszPath,
128 uint dwFileAttributes,
129 ref ShellFileInfo psfi,
130 uint cbFileInfo,
131 uint uFlags
132 );
133
134 ///
135 /// lookup and return an icon from windows shell
136 ///
137 /// "name">path to the file
138 /// "size">large or small
139 /// "linkOverlay">true to include the overlay link iconlet
140 /// requested icon
141 public static System.Drawing.Icon GetFileIcon(
142 string filePath,
143 EnumIconSize size,
144 bool addLinkOverlay)
145 {
146
147 EnumFileInfoFlags flags =
148 EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES;
149
150 // add link overlay if requested
151 if (addLinkOverlay) {
152 flags |= EnumFileInfoFlags.LINKOVERLAY;
153 }
154
155 // set size
156 if (size == EnumIconSize.Small) {
157 flags |= EnumFileInfoFlags.SMALLICON;
158 }
159 else {
160 flags |= EnumFileInfoFlags.LARGEICON;
161 }
162
163 ShellFileInfo shellFileInfo = new ShellFileInfo();
164
165 SHGetFileInfo(
166 filePath,
167 conFILE_ATTRIBUTE_NORMAL,
168 ref shellFileInfo,
169 (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo),
170 (uint)flags);
171
172 // deep copy
173 System.Drawing.Icon icon =
174 (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone();
175
176 // release handle
177 DestroyIcon(shellFileInfo.hIcon);
178
179 return icon;
180 }
181
182 ///
183 /// lookup and return an icon from windows shell
184 ///
185 /// "size">large or small
186 /// "folderType">open or closed
187 /// requested icon
188 public static System.Drawing.Icon GetFolderIcon(
189 EnumIconSize size,
190 EnumFolderType folderType)
191 {
192 return GetFolderIcon(null, size, folderType);
193 }
194
195 ///
196 /// lookup and return an icon from windows shell
197 ///
198 /// "folderPath">path to folder
199 /// "size">large or small
200 /// "folderType">open or closed
201 /// requested icon
202 public static System.Drawing.Icon GetFolderIcon(
203 string folderPath,
204 EnumIconSize size,
205 EnumFolderType folderType)
206 {
207
208 EnumFileInfoFlags flags =
209 EnumFileInfoFlags.ICON | EnumFileInfoFlags.USEFILEATTRIBUTES;
210
211 if (folderType == EnumFolderType.Open) {
212 flags |= EnumFileInfoFlags.OPENICON;
213 }
214
215 if (EnumIconSize.Small == size) {
216 flags |= EnumFileInfoFlags.SMALLICON;
217 }
218 else {
219 flags |= EnumFileInfoFlags.LARGEICON;
220 }
221
222 ShellFileInfo shellFileInfo = new ShellFileInfo();
223 SHGetFileInfo(
224 folderPath,
225 conFILE_ATTRIBUTE_DIRECTORY,
226 ref shellFileInfo,
227 (uint)System.Runtime.InteropServices.Marshal.SizeOf(shellFileInfo),
228 (uint)flags );
229
230 // deep copy
231 System.Drawing.Icon icon =
232 (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shellFileInfo.hIcon).Clone();
233
234 // release handle
235 DestroyIcon(shellFileInfo.hIcon);
236
237 return icon;
238 }
239 }
240}