This repository was archived by the owner on Jul 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigurationManager.cs
61 lines (54 loc) · 1.93 KB
/
ConfigurationManager.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace IniFiles
{
public class ConfigurationManager
{
private Dictionary<string,Dictionary<string, string>> dico;
private DateTime lastUpdate = DateTime.MinValue;
private string iniPath;
private static string slash1 = Path.DirectorySeparatorChar.ToString();
private static string slash2 = slash1 + slash1;
public ConfigurationManager(string iniPath)
{
this.iniPath = iniPath;
refresh(true);
}
public string getValue(string section, string key)
{
refresh();
if (dico.ContainsKey(section) && dico[section].ContainsKey(key))
return dico[section][key].Replace(slash2,slash1);
return string.Empty;
}
public void setValue(string section, string key,string value)
{
IniWriter.WriteKey(iniPath, section, key, value.Replace(slash1,slash2));
lastUpdate = DateTime.MinValue;
}
private void refresh()
{
refresh(false);
}
private void refresh(bool force)
{
FileInfo fi = new FileInfo(iniPath);
if (force || fi.LastWriteTime > lastUpdate)
{
Dictionary<string, Dictionary<string, string>> tmpDico = new Dictionary<string, Dictionary<string, string>>();
foreach(string category in IniReader.GetCategories(iniPath))
{
tmpDico.Add(category, new Dictionary<string, string>());
foreach(string key in IniReader.GetKeys(iniPath, category))
{
tmpDico[category].Add(key, IniReader.GetValue(iniPath, category, key, ""));
}
}
dico = tmpDico;
lastUpdate = DateTime.Now;
}
}
}
}