-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFolderAnalyzer.cs
More file actions
executable file
·173 lines (148 loc) · 4.36 KB
/
FolderAnalyzer.cs
File metadata and controls
executable file
·173 lines (148 loc) · 4.36 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
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
using System;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Windows.Forms;
namespace DiskUsageAnalyzer
{
public class FolderAnalyzer
{
int mapdepth = 0;
string path = "";
BackgroundWorker worker;
//Depth of folder-map (for zoom)
public int MapDepth { get { return mapdepth;} set{mapdepth = value;} }
//Constructor
public FolderAnalyzer(string path, BackgroundWorker worker)
{
this.path = path;
this.worker = worker;
}
//Analyzes the folder given when the object was created and calculate disk.
public FolderItem AnalyzeFolder()
{
TreeNode rootTreeNode = new TreeNode(ShortFolderName(path));
FolderItem root = new FolderItem(path, rootTreeNode);
rootTreeNode.Tag = root;
TraverseFolder(root);
root.PercentageOfParent = 1f;
root.StartAngle = 0f;
root.SweepAngle = 1f;
root.Level = 1;
CalculateDisk(root);
return root;
}
//Recursively travese the folder structure.
public void TraverseFolder( FolderItem root )
{
try
{
DirectoryInfo dirinfo = new DirectoryInfo(root.FolderName);
worker.ReportProgress(0, dirinfo.FullName);
root.Size = root.TotalSize = CalculateSize(dirinfo);
foreach (DirectoryInfo folder in dirinfo.GetDirectories())
{
TreeNode newNode = new TreeNode(ShortFolderName(folder.FullName));
FolderItem child = new FolderItem(folder.FullName, newNode);
newNode.Tag = child;
TraverseFolder(child);
root.TotalSize += child.TotalSize;
root.Children.Add(child);
root.FolderNode.Nodes.Add(child.FolderNode);
}
}
catch (UnauthorizedAccessException)
{
//If we cannot traverse further because of the directory access rules then stop.
return;
}
catch (PathTooLongException)
{
}
}
//Calculates the size in bytes of a folder.
private static long CalculateSize( DirectoryInfo info )
{
try
{
long size = 0;
foreach (FileInfo file in info.GetFiles())
size += file.Length;
return size;
}
catch (UnauthorizedAccessException)
{
//Didn't have access, the size of the folder is then 0 bytes
return 0;
}
catch (FileNotFoundException)
{
//File somehow not found, adds 0 to the size;
return 0;
}
}
//Calculate pie-slice sizes and orientations.
public void CalculateDisk(FolderItem root)
{
float angle = root.StartAngle;
float sweep = root.SweepAngle;
int level = root.Level;
foreach (FolderItem child in root.Children)
{
child.PercentageOfParent = (float)child.TotalSize / (float)root.TotalSize;
child.Level = level + 1;
child.StartAngle = Math.Abs( angle );
child.SweepAngle = Math.Abs(child.PercentageOfParent * sweep);
CalculateDisk(child);
angle += child.SweepAngle;
}
if( root.SweepAngle > 0.001 )
mapdepth = Math.Max(mapdepth, root.Level);
}
//Static helper methods:
//Format a size in bytes into a string of human readable sizes.
public static string FormatFileSize(long sizeInBytes )
{
StringBuilder b = new StringBuilder();
long gb = 1024 * 1024 * 1024;
long mb = 1024 * 1024;
long kb = 1024;
//Number that decrements every time a number is appended to keep the tooltips less cluttered.
int numinfo = 2;
if (sizeInBytes > gb && numinfo > 0)
{
b.Append((int)(sizeInBytes / gb) + "gb " );
sizeInBytes %= gb;
numinfo--;
}
if (sizeInBytes > mb && numinfo > 0)
{
b.Append((int)(sizeInBytes / mb) + "mb ");
sizeInBytes %= mb;
numinfo--;
}
if (sizeInBytes > kb && numinfo > 0)
{
b.Append((int)(sizeInBytes / kb) + "kb ");
sizeInBytes %= kb;
numinfo--;
}
if (sizeInBytes > 0 && numinfo > 0)
{
b.Append( sizeInBytes + "b");
}
if (string.IsNullOrEmpty(b.ToString()))
b.Append("0b");
return b.ToString().TrimEnd();
}
//Returns the last folder name of a full path.
public static string ShortFolderName(string fullpath)
{
string path = fullpath.Substring(fullpath.LastIndexOf(Path.DirectorySeparatorChar)+1);
if (string.IsNullOrEmpty(path.Trim()))
return fullpath;
else
return path;
}
}
}