-
Notifications
You must be signed in to change notification settings - Fork 0
/
USBDevice.cs
128 lines (114 loc) · 3.49 KB
/
USBDevice.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
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
using System.Text.RegularExpressions;
using Core;
namespace USB
{
class USBDevice
{
public static List<string> GetList()
{
var devicesListFromLsbls = CoreCommands.ExecShellCommand("lsblk", "--output NAME --noheadings --nodeps");
List<string> devices = Regex.Replace(devicesListFromLsbls, "sr[0-9]|cdrom[0-9]", "")
.Split()
.Where(IsRemovable)
.Select(x => "/dev/" + x)
.ToList();
if (devices.Count >= 1)
{
return devices;
}
else
{
devices.Add("None");
return devices;
}
}
public static float GetUsbDeviceCapacity(string device)
{
var returned = 0f;
if (device != "")
{
var output = CoreCommands.ExecShellCommand("lsblk", "--output SIZE --noheadings --nodeps " + device)
.Trim();
if (output != "")
{
returned = float.Parse(Regex.Replace(output, "G", ""));
}
else
{
returned = 0f;
}
}
else
{
returned = 0f;
}
return returned;
}
public static string GetUsbDeviceModel(string device)
{
var returned = "Empty device variable";
if (device != "")
{
var output = CoreCommands.ExecShellCommand("lsblk", "--output MODEL --noheadings --nodeps " + device)
.Trim();
if (output != "")
{
returned = output;
}
else
{
returned = "Not found device";
}
}
else
{
returned = "Empty device variable";
}
return returned;
}
public static bool IsMounted(string device)
{
var returned = device != "" ? CoreCommands.ExecShellCommand("df").Contains(device) : false;
return returned;
}
public static int MountDevice(string device, string mountpoint)
{
if (IsMounted(device))
{
return 1;
}
else
{
CoreCommands.ExecShellCommand("mount", device + " " + mountpoint);
return 0;
}
}
public static int UnmountDevice(string device)
{
if (IsMounted(device))
{
for (int i = 0; i <= 9; i++)
{
CoreCommands.ExecShellCommand("umount", device + i.ToString());
}
return 0;
}
else
{
return 1;
}
}
public static bool IsRemovable(string device)
{
var returnedValue = false;
var sysfsBlockDeviceDir = "/sys/block/" + device;
if (File.Exists(sysfsBlockDeviceDir + "/removable"))
{
var removableContent = File.ReadAllText("/sys/block/" + device + "/removable").Trim();
// var ro_content = File.ReadAllText("/sys/block/" + device + "/ro").Trim();
returnedValue = removableContent == "1";
}
return returnedValue;
}
}
}