-
Notifications
You must be signed in to change notification settings - Fork 5
/
DirectoryNode.cs
88 lines (74 loc) · 2.6 KB
/
DirectoryNode.cs
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
using Microsoft.Win32.Interop;
using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace IMAPI2
{
public class DirectoryNode : IMediaNode
{
private Image _fileIcon;
private string _name;
private List<IMediaNode> _nodes;
private string _path;
private long _sizeOnDisc;
public string Name
{
get { return _name; }
}
public List<IMediaNode> Nodes
{
get { return _nodes; }
}
public string Path
{
get { return _path; }
}
public long SizeOnDisc
{
get { return _sizeOnDisc; }
}
public Image FileIcon
{
get { return _fileIcon; }
}
public DirectoryNode(string path)
{
if (!Directory.Exists(path))
throw new FileNotFoundException("The directory added to MediaDirectory was not found.", path);
_path = path;
_nodes = new List<IMediaNode>();
_name = new FileInfo(_path).Name;
string[] files = Directory.GetFiles(_path);
foreach (string file in files)
_nodes.Add(new FileNode(file));
string[] directories = Directory.GetDirectories(_path);
foreach (string directory in directories)
_nodes.Add(new DirectoryNode(directory));
_sizeOnDisc = 0;
foreach (IMediaNode node in _nodes)
_sizeOnDisc += node.SizeOnDisc;
//
// Get the Directory icon
//
Shell.SHFILEINFO shinfo = new Shell.SHFILEINFO();
IntPtr hImg = Shell.SHGetFileInfo(_path, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo), Shell.SHGFI_ICON | Shell.SHGFI_SMALLICON);
if (shinfo.hIcon != null)
{
//The icon is returned in the hIcon member of the shinfo struct
System.Drawing.IconConverter imageConverter = new System.Drawing.IconConverter();
System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
try
{
_fileIcon = (System.Drawing.Image)imageConverter.ConvertTo(icon, typeof(System.Drawing.Image));
}
catch (NotSupportedException)
{
}
Shell.DestroyIcon(shinfo.hIcon);
}
}
}
}