-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDriveUtils.cs
More file actions
60 lines (52 loc) · 1.49 KB
/
DriveUtils.cs
File metadata and controls
60 lines (52 loc) · 1.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
namespace SystemTools.Utils;
public static class DriveUtils
{
private static string GetDriveJsonPath()
{
var pluginDir = Path.GetDirectoryName(typeof(DriveUtils).Assembly.Location);
return Path.Combine(pluginDir, "drive.json");
}
public static List<string> GetCurrentDrives()
{
return DriveInfo.GetDrives()
.Where(d => d.IsReady)
.Select(d => d.Name.TrimEnd('\\'))
.ToList();
}
public static List<string> LoadSavedDrives()
{
var path = GetDriveJsonPath();
if (!File.Exists(path))
return new List<string>();
try
{
var json = File.ReadAllText(path);
return JsonSerializer.Deserialize<List<string>>(json) ?? new List<string>();
}
catch
{
return new List<string>();
}
}
public static void SaveDrives(List<string> drives)
{
var path = GetDriveJsonPath();
var json = JsonSerializer.Serialize(drives);
File.WriteAllText(path, json);
}
public static void InitializeDriveRecord()
{
var currentDrives = GetCurrentDrives();
SaveDrives(currentDrives);
}
public static List<string> GetNewDrives(List<string> previousDrives)
{
var currentDrives = GetCurrentDrives();
return currentDrives.Except(previousDrives).ToList();
}
}