Repository navigation
Expand file tree
/
Copy pathConfigManager.cs
More file actions
74 lines (69 loc) · 2.18 KB
/
Copy pathConfigManager.cs
File metadata and controls
74 lines (69 loc) · 2.18 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace ProcessFreezer
{
/// <summary>
/// 配置管理器
/// </summary>
public class ConfigManager
{
private static readonly string ConfigPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"ProcessFreezer",
"config.json");
/// <summary>
/// 应用配置
/// </summary>
public class AppConfig
{
public List<string> MonitoredProcesses { get; set; } = new();
public int ScanIntervalSeconds { get; set; } = 30;
public int MemoryThresholdMB { get; set; } = 10;
public bool AutoStart { get; set; } = true;
public bool LowerPriorityForMonitored { get; set; } = false;
public int ReTrimDeltaMB { get; set; } = 40;
}
/// <summary>
/// 加载配置
/// </summary>
public static AppConfig Load()
{
try
{
if (File.Exists(ConfigPath))
{
var json = File.ReadAllText(ConfigPath);
return JsonSerializer.Deserialize<AppConfig>(json) ?? new AppConfig();
}
}
catch
{
// 配置损坏,返回默认
}
return new AppConfig();
}
/// <summary>
/// 保存配置
/// </summary>
public static void Save(AppConfig config)
{
try
{
var dir = Path.GetDirectoryName(ConfigPath);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var options = new JsonSerializerOptions { WriteIndented = true };
var json = JsonSerializer.Serialize(config, options);
File.WriteAllText(ConfigPath, json);
}
catch
{
// 保存失败,忽略
}
}
}
}