-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopSorterUtilities.cs
More file actions
77 lines (74 loc) · 2.49 KB
/
Copy pathDesktopSorterUtilities.cs
File metadata and controls
77 lines (74 loc) · 2.49 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace DesktopSorter
{
/// <summary>
/// Various misc. methods for
/// the desktopSorter
/// </summary>
class DesktopSorterUtilities
{
/// <summary>
/// Gets the current desktop size in bytes
/// </summary>
/// <returns>float bytes</returns>
public static float desktopSize()
{
string[] files = Directory.GetFiles(DesktopSorterVariables.desktopPath);
float desktopSize = 0;
foreach(var f in files)
{
FileInfo fileInfo = new FileInfo(f);
desktopSize += fileInfo.Length;
}
return desktopSize;
}
/// <summary>
/// Gets the current directory size
/// provided, and returns the total bytes
/// </summary>
/// <param name="directory"></param>
/// <param name="inGigabytes"></param>
/// <returns>float bytes</returns>
public static float directorySize(string directory, bool inGigabytes = true)
{
try
{
float size = 0;
DirectoryInfo d = new DirectoryInfo(directory);
FileInfo[] fis = d.GetFiles();
DirectoryInfo[] dis = d.GetDirectories();
foreach (FileInfo fi in fis)
size += fi.Length;
foreach (DirectoryInfo di in dis)
size += directorySize(di.FullName, inGigabytes: false);
return size;
}
catch
{
return 0;
}
}
/// <summary>
/// Converts bytes to a human-readable form
/// </summary>
/// <param name="byteCount"></param>
/// <returns>string representation</returns>
public static String bytesToString(long byteCount)
{
string[] suffixes = {
"B", "KB", "MB",
"GB", "TB", "PB",
"EB"
};
if (byteCount == 0)
return "0" + suffixes[0];
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return (Math.Sign(byteCount) * num).ToString() + suffixes[place];
}
}
}