This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UtilsHelper.cs
58 lines (48 loc) · 1.63 KB
/
UtilsHelper.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
using System.Diagnostics;
namespace DriveFiller
{
internal static class UtilsHelper
{
internal static double ExecutionTime(Action act)
{
Stopwatch watch = new();
watch.Restart();
act.Invoke();
watch.Stop();
return watch.Elapsed.TotalMilliseconds;
}
internal static async Task<double> ExecutionTimeAsync(Func<Task> func)
{
Stopwatch watch = new();
watch.Restart();
await func.Invoke();
watch.Stop();
return watch.Elapsed.TotalMilliseconds;
}
internal static byte[] GenerateRandomBytes(int size)
{
char[] characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_".ToCharArray();
Random random = new();
byte[] bytes = new byte[size];
for (int i = 0; i < size; i++)
{
bytes[i] = (byte)characters[random.Next(characters.Length)];
}
return bytes;
}
internal static string GenerateRandomName()
{
return "drivefiller_" + Path.GetRandomFileName().Replace(".", "");
}
internal static string ConvertStorage(int size)
{
return size switch
{
>= 1024 and < 1024000 => string.Concat(size / 1024, " KB"),
>= 1024000 and < 1024000000 => string.Concat(size / 1024 / 1024, " MB"),
>= 1024000000 => string.Concat(size / 1024 / 1024 / 1024, " GB"),
_ => string.Concat(size, " B"),
};
}
}
}