Repository navigation
Expand file tree
/
Copy pathSettings.cs
More file actions
114 lines (96 loc) · 2.34 KB
/
Copy pathSettings.cs
File metadata and controls
114 lines (96 loc) · 2.34 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
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace TweetdeckSucks
{
public class Settings
{
private readonly string filename;
private Dictionary<string, string> keys;
public Settings(String filename)
{
this.filename = filename;
this.Load();
}
private void Load()
{
if (File.Exists(this.filename)) {
this.keys = File.ReadLines(this.filename)
.Select(s => s.Split(new[] {'='}, 2))
.Where(strings => strings.Length == 2)
.ToDictionary(strings => strings[0], strings => strings[1]);
}
}
public void Save()
{
File.WriteAllLines(this.filename, this.keys.Select(pair => string.Format("{0}={1}", pair.Key, pair.Value)));
}
public void Cancel()
{
this.Load();
}
public string GetFilename()
{
return this.filename;
}
public string GetString(string key)
{
return this.keys.ContainsKey(key) ? this.keys[key] : "";
}
public bool GetBool(string key)
{
try {
if (this.keys.ContainsKey(key)) {
return bool.Parse(this.keys[key]);
}
} catch {}
return false;
}
public int GetInt(string key)
{
try {
if (this.keys.ContainsKey(key)) {
return int.Parse(this.keys[key]);
}
} catch {}
return 0;
}
public float GetFloat(string key)
{
try {
if (this.keys.ContainsKey(key)) {
return float.Parse(this.keys[key], CultureInfo.InvariantCulture.NumberFormat);
}
} catch {}
return 0f;
}
public bool Contains(string key)
{
return this.keys.ContainsKey(key);
}
public void SetString(string key, string value)
{
this.keys[key] = value;
}
public void SetBool(string key, bool value)
{
this.keys[key] = value.ToString();
}
public void SetInt(string key, int value)
{
this.keys[key] = value.ToString(CultureInfo.InvariantCulture);
}
public void SetFloat(string key, float value)
{
this.keys[key] = value.ToString(CultureInfo.InvariantCulture);
}
public void Delete(string key)
{
if (this.keys.ContainsKey(key)) {
this.keys.Remove(key);
}
}
}
}