-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigFile.cs
82 lines (69 loc) · 2.12 KB
/
ConfigFile.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
using System;
using System.Collections.Generic;
using System.IO;
namespace Kraken
{
/// <summary>
/// A simple and crappy configuration file class.
/// </summary>
class ConfigFile
{
private FileStream file;
private Dictionary<string, string> entries = new Dictionary<string, string>();
/// <summary>
/// Constructs a ConfigFile object, loading configuration from specified file
/// </summary>
public ConfigFile(string filename)
{
file = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamReader sr = new StreamReader(file);
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (!line.Contains("="))
continue;
string[] arr = line.Split("=".ToCharArray(), 2);
entries[arr[0]] = arr[1];
}
}
/// <summary>
/// Returns an integer from the configuration
/// </summary>
public int GetInt(string key)
{
if (!entries.ContainsKey(key))
return -1;
string val = entries[key];
return Int32.Parse(val);
}
/// <summary>
/// Sets the integer value of an entry in the configuration
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void SetInt(string key, int value)
{
entries[key] = value.ToString();
}
/// <summary>
/// Writes configuration back to the originally loaded file
/// </summary>
public void Save()
{
StreamWriter sw = new StreamWriter(file);
file.SetLength(0);
foreach (KeyValuePair<string, string> kvp in entries)
{
sw.WriteLine(kvp.Key + "=" + kvp.Value);
}
sw.Flush();
}
/// <summary>
/// Closes associated file.
/// </summary>
public void Close()
{
file.Close();
}
}
}