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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
#region Win32 Klassen
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using MimetypeHelper.Properties;
/// <summary> Summary description for NativeIcon.</summary>
public class NativeIcon
{
#region WIN32
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHGetFileInfo(
string pszPath,
int dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags);
/// <summary>Maximal Length of unmanaged Windows-Path-strings</summary>
private const int MAX_PATH = 260;
/// <summary>Maximal Length of unmanaged Typename</summary>
private const int MAX_TYPE = 80;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero;
iIcon = 0;
dwAttributes = 0;
szDisplayName = "";
szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_TYPE)]
public string szTypeName;
};
private NativeIcon()
{
}
[Flags]
enum SHGFI : int
{
/// <summary>get icon</summary>
Icon = 0x000000100,
/// <summary>get display name</summary>
DisplayName = 0x000000200,
/// <summary>get type name</summary>
TypeName = 0x000000400,
/// <summary>get attributes</summary>
Attributes = 0x000000800,
/// <summary>get icon location</summary>
IconLocation = 0x000001000,
/// <summary>return exe type</summary>
ExeType = 0x000002000,
/// <summary>get system icon index</summary>
SysIconIndex = 0x000004000,
/// <summary>put a link overlay on icon</summary>
LinkOverlay = 0x000008000,
/// <summary>show icon in selected state</summary>
Selected = 0x000010000,
/// <summary>get only specified attributes</summary>
Attr_Specified = 0x000020000,
/// <summary>get large icon</summary>
LargeIcon = 0x000000000,
/// <summary>get small icon</summary>
SmallIcon = 0x000000001,
/// <summary>get open icon</summary>
OpenIcon = 0x000000002,
/// <summary>get shell size icon</summary>
ShellIconSize = 0x000000004,
/// <summary>pszPath is a pidl</summary>
PIDL = 0x000000008,
/// <summary>use passed dwFileAttribute</summary>
UseFileAttributes = 0x000000010,
/// <summary>apply the appropriate overlays</summary>
AddOverlays = 0x000000020,
/// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary>
OverlayIndex = 0x000000040,
}
#endregion
/// <summary>
/// Get the associated Icon for a file or application, this method always returns
/// an icon. If the strPath is invalid or there is no idonc the default icon is returned
/// </summary>
/// <param name="strPath">full path to the file</param>
/// <param name="bSmall">if true, the 16x16 icon is returned otherwise the 32x32</param>
/// <returns></returns>
public static Icon GetIcon(string strPath, bool bSmall)
{
try
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;
SHGetFileInfo(strPath, 0, out info, (uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
catch (Exception)
{
// dann ist bei der Extrahierung was schief gegangen und wir geben das Default Icon zurück ..
return Resources.undefined;
}
}
}
#endregion
#region Dateityp Klasse
namespace MimetypeHelper
{
/// <summary>
/// Diese Klasse stellt einen Dateityp dar.
/// Sie besitzt die Eigenschaften der Erweiterung, die 'Content Type' Beschreibung und das zugehörige Icon im Format 32x32 Pixel.
/// (C) Patrick Sperneder 2008
/// </summary>
public class mimetype
{
#region Member
private System.Drawing.Icon ico;
private string mime;
private string extension;
#endregion
#region Konstruktor
public mimetype(System.Drawing.Icon i, string m, string ext)
{
this.ico = i;
this.mime = m;
this.extension = ext;
}
#endregion
#region Props
public System.Drawing.Icon MimeIcon { get { return this.ico; } }
public string MimeTypeString { get { return this.mime; } }
public string Extension { get { return this.extension; } }
#endregion
}
}
#endregion
#region Dateitypen Manager Klasse
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using MimetypeHelper.Properties;
namespace MimetypeHelper
{
/// <summary>
/// Diese Klasse liest aus der Registry alle registrierten Dateitypen die eine Inhaltsbeschreibung haben
/// und ermittelt deren Icons und stellt dies in einer generischen Liste zur Verfügung.
/// TODO : Das Laden der Icons aus der erstellten Textliste (aus Performance Gründen ) , ist qualitativ nicht zufriedenstellend.
/// Derzeit wird bei jeder Instanzierung dieser Klasse, die Dateitypen neu erstellt. (Bug#TODO#1710)
/// (C) Sperneder Patrick 2008.
/// Wenn Ihr die Klasse gut findet, wäre ich für ein paar Punkte dankbar.
/// </summary>
public class MimetypePool
{
#region Member
private const string MIME_RESSOURCE_FILE = "MIME.TYPES";
private List<mimetype> types;
#endregion
#region Konstruktor
public MimetypePool()
{
if (!IsMimeListStored())
{
this.types = GetMimetypes();
// nachdem wir sie jetzt aufwändig erzeugt haben,
// ersparen wir uns das beim nächsten mal und speichern sie lokal ab.
StoreMimeList();
}
else
{
// die icons sehen einfach nicht schön aus !
//this.types = RestoreMimeList();
this.types = GetMimetypes();
}
}
#endregion
#region Speichern Laden der Liste
/// <summary>
/// Überprüft ob die Liste vorhanden ist oder nicht.
/// </summary>
/// <returns></returns>
private bool IsMimeListStored()
{
if (File.Exists(MIME_RESSOURCE_FILE))
return true;
else
return false;
}
/// <summary>
/// Speichert die Mimeliste im Pfad der Anwendung ab, um neues erstellen zu vermeiden.
/// </summary>
private void StoreMimeList()
{
StringBuilder flbuild = new StringBuilder();
foreach (mimetype m in this.types)
{
flbuild.AppendLine(IconToBase64String(m.MimeIcon) + ";" + m.MimeTypeString + ";" + m.Extension);
}
File.WriteAllText(MIME_RESSOURCE_FILE, flbuild.ToString());
}
/// <summary>
/// Erstellt aus der Datei eine neue MIMEListe im Pfad der Anwendung..
/// </summary>
/// <returns></returns>
private List<mimetype> RestoreMimeList()
{
try
{
List<mimetype> m = new List<mimetype>();
string[] lines = File.ReadAllLines(MIME_RESSOURCE_FILE);
foreach (string line in lines)
{
string[] pcs = line.Split(new char[] { ';' });
m.Add(new mimetype(IconFromBase64String(pcs[0]), pcs[1], pcs[2]));
}
return m;
}
catch (Exception)
{
return GetDefaultListOnError();
}
}
/// <summary>
/// Sollte bei der Ermittlung der Dateitypen etwas schief gelaufen sein,
/// so wird diese liste mit den Dateitypen und deren icons zurückgegeben.
/// (Zwar nicht vollständig, aber ausreichend im Fehlerfall)
/// </summary>
/// <returns></returns>
private List<mimetype> GetDefaultListOnError()
{
List<mimetype> m = new List<mimetype>();
m.Add(new mimetype(Resources.doc, "Microsoft Word Document", ".doc"));
m.Add(new mimetype(Resources.jpg, "JPG image", ".jpg"));
m.Add(new mimetype(Resources.zip, "Compressed archive", ".zip"));
// ...
return m;
}
#endregion
#region Erzeugen der Liste
/// <summary>
/// Erstellt die generische Liste mit den Dateitypen auf dem Rechner.
/// </summary>
/// <returns></returns>
private List<mimetype> GetMimetypes()
{
List<mimetype> ret = new List<mimetype>();
try
{
//alle vorhandenen Dateitypen durchsuchen.
foreach (string key in Registry.ClassesRoot.GetSubKeyNames())
{
// nur extensions ...
if (key.StartsWith("."))
{
// nur wenn der Datei auch ein Icon zugeordnet ist.
if (HasAssociatedIcon(GetExtensionDefaultValue(key)))
{
ret.Add(new mimetype(NativeIcon.GetIcon(key, false), GetExtensionDisplayDescription(GetExtensionDefaultValue(key)), key));
}
}
}
return ret;
}
catch (Exception)
{
return GetDefaultListOnError();
}
}
/// <summary>
/// Gibt den Wert des (Default) Wertes eines Registry Schlüssels zurück.
/// </summary>
/// <param name="Extension">Die Extension mit führendem Punkt. z.B .txt ...</param>
/// <returns>Die Beschreb</returns>
private string GetExtensionDefaultValue(string Extension)
{
string def = (string)Registry.ClassesRoot.OpenSubKey(Extension).GetValue("");
if (def == null || def == string.Empty)
return "n";
else
return def;
}
/// <summary>
/// Gibt zurück ob dieser Endung ein Icon zugeordnet ist oder nicht.
/// </summary>
/// <param name="ExtensionDefaultValue">Der Standard Wert der Erweiterung</param>
/// <returns></returns>
private bool HasAssociatedIcon(string ExtensionDefaultValue)
{
try
{
if (ExtensionDefaultValue == "n")
return false;
// Überprüfen ob der Schlüssel überhaupt vorhanden ist ..
if (Registry.ClassesRoot.OpenSubKey(ExtensionDefaultValue) != null)
{
if (Registry.ClassesRoot.OpenSubKey(ExtensionDefaultValue).OpenSubKey("defaulticon") == null)
return false;
else
return true;
}
else
return false;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Gibt die Klartext-Beschreibung der Extension zurück. für z.b (.txt) Word Textdokument ...)
/// </summary>
/// <param name="ExtensionDefaultValue">Der Defaultwert eines extension Schlüssel</param>
/// <returns></returns>
private string GetExtensionDisplayDescription(string ExtensionDefaultValue)
{
if (ExtensionDefaultValue == "n")
return "n";
string desc = (string)Registry.ClassesRoot.OpenSubKey(ExtensionDefaultValue).GetValue("");
if (desc == null || desc == string.Empty)
{
return "keine Beschreibung";
}
else
{
return desc;
}
}
#endregion
#region Zusatzfunktionen
public Icon IconFromBase64String(string base64)
{
MemoryStream memory = new MemoryStream(Convert.FromBase64String(base64));
Icon result = new Icon(memory, new Size(32, 32));
memory.Close();
return result;
}
public string IconToBase64String(Icon image)
{
MemoryStream memory = new MemoryStream();
image.Save(memory);
string base64 = Convert.ToBase64String(memory.ToArray());
memory.Close();
return base64;
}
#endregion
#region Methoden
/// <summary>
/// Gibt das Objekt das der Dateinamenserweiterung entspricht zurück.
/// </summary>
/// <param name="Extension">Die Dateinamenserweiterung der Datei inklusive führenedem Punkt. z.B .txt</param>
/// <returns>Bei Erfolg das Objekt, andernfalls nix.</returns>
public mimetype GetTypeByExtension(string Extension)
{
Extension = Extension.ToLower();
for (int i = this.types.Count - 1; i >= 0; i--)
{
if (types[i].Extension == Extension)
{
return types[i];
}
}
// wenn wir nichts finden, geben wir einen unbekannten Dateitypen zurück
return new mimetype(Resources.undefined, "unbekannte Datei", "???");
}
/// <summary>
/// Gibt das Symbol für einen Ordner zurück
/// </summary>
/// <returns></returns>
public mimetype GetFolder()
{
return new mimetype(Resources.folder, "Ordner", "keine");
}
/// <summary>
/// Gibt das Symbol für einen Link zurück
/// </summary>
/// <returns></returns>
public mimetype GetLink()
{
return new mimetype(Resources.link , "Link", "Verknüpfung");
}
#endregion
#region Eigenschaften
/// <summary>
/// Gibt alle Dateitypen die eine Beschreibung haben zurück. (andernfalls eine standardliste)
/// </summary>
public List<mimetype> MimeTypes { get { return this.types; } }
#endregion
}
}
#endregion
|