-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationSettings.cs
129 lines (118 loc) · 3.59 KB
/
ApplicationSettings.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
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// ApplicationSettings
// - KEY-VALUE pairs
// - VALUE Types (e.g. string, numeric, bool)
// - Sections (MASS_YAML, UPDATE_YAML, etc..)
// - SaveToIniFile()
// - LoadFromIniFile()
// - TAB Button Fonts
// - Microsoft bla bla bla, font size,
//
namespace CommonForms
{
public class ApplicationSettings
{
// Dictionary to store the settings
private Dictionary<string, object> _settings = new Dictionary<string, object>();
// Add or update a key-value pair
public void Add(string key, object value)
{
if (_settings.ContainsKey(key))
{
_settings[key] = value; // Update existing key
}
else
{
_settings.Add(key, value); // Add new key
}
}
// Remove a key-value pair
public bool Remove(string key)
{
return _settings.Remove(key);
}
// Clear all key-value pairs
public void Clear()
{
_settings.Clear();
}
// Save settings to an INI file
public void SaveToIni(string filePath)
{
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
foreach (var pair in _settings)
{
writer.WriteLine($"{pair.Key}={ConvertToString(pair.Value)}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error saving to INI file: {ex.Message}");
}
}
// Load settings from an INI file
public void LoadFromIni(string filePath)
{
try
{
if (!File.Exists(filePath)) return;
foreach (var line in File.ReadLines(filePath))
{
var parts = line.Split(new char[] { '=' }, 2);
if (parts.Length == 2)
{
var key = parts[0].Trim();
var value = ConvertToObject(parts[1].Trim());
Add(key, value);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading from INI file: {ex.Message}");
}
}
// Helper method to convert an object to a string for saving
private string ConvertToString(object value)
{
if (value is bool)
{
return ((bool)value) ? "true" : "false";
}
return value.ToString(); // Default for other types
}
// Helper method to convert a string to the correct object type
private object ConvertToObject(string value)
{
if (bool.TryParse(value, out bool boolValue))
{
return boolValue;
}
if (int.TryParse(value, out int intValue))
{
return intValue;
}
if (float.TryParse(value, out float floatValue))
{
return floatValue;
}
return value; // Default is string
}
// Optional: Print all settings (for debugging purposes)
public void PrintSettings()
{
foreach (var pair in _settings)
{
Console.WriteLine($"{pair.Key}: {pair.Value}");
}
}
}
};