-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFSInfoGetter.cs
60 lines (59 loc) · 2.38 KB
/
FSInfoGetter.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
using System.Collections.Generic;
using System.IO;
using System;
namespace gtkfetch
{
class Drive
{
public DriveInfo driveInfo;
public string driveName;
public string sizeUnit;
public Decimal totalSize;
public Decimal usePercent;
/// <summary> Constructor that creates a Drive instance which contains info about a Drive </summary>
public Drive(string drivename)
{
DriveInfo drive = new DriveInfo(drivename);
driveInfo = drive;
driveName = drivename;
// is the drive over 1024GiB? if so, totalsize is going to be displayed as TiB; 1024^4 = 1099511627776
if (drive.TotalSize >= 1099511627776)
{
totalSize = drive.TotalSize/1099511627776;
sizeUnit = "TiB";
}
// is the drive over 1GiB? if so, totalsize is going to be displayed as GiB; 1024^3 = 1073741824
else if (drive.TotalSize >= 1073741824)
{
totalSize = drive.TotalSize/1073741824;
sizeUnit = "GiB";
}
// otherwise, display the drive in MiB; 1024^2 = 1048576
else
{
totalSize = drive.TotalSize/1048576;
sizeUnit = "MiB";
}
usePercent = Math.Round((decimal)(drive.TotalSize - drive.TotalFreeSpace) / drive.TotalSize * 100, 1);
}
}
class FSInfoGetter
{
static string driveexpr = @"^(?!#)[a-zA-Z0-9\/=\-\""]*\s*(?!none)(\S*)";
static List<string> drivenames = new List<string>(FileReader.ReadFileMatchMultiple("/etc/fstab", driveexpr, 1));
/// <summary> Creates labels containing drive info and adds them to array </summary>
static public void GetDrives()
{
foreach (string d in drivenames)
{
Drive drive = new Drive(d);
if (drive.driveInfo.IsReady && drive.driveInfo.DriveType != DriveType.CDRom)
{
InfoLabel driveLabel = new InfoLabel(drive.driveName, "drive-harddisk");
Labels.labels.Add(driveLabel);
driveLabel.contentLabel.Text = $"total: {drive.totalSize}{drive.sizeUnit}, {drive.usePercent}% used";
}
}
}
}
}