-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.cs
156 lines (120 loc) · 4.4 KB
/
Settings.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Linq;
using System.Xml.Linq;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace TripCalculator
{
class Settings
{
private string settingsFileName = "";
private XDocument docSettings;
public Settings()
{
}
public Settings(string fileName)
{
settingsFileName = fileName;
if (!File.Exists(settingsFileName)) CreateNew();
docSettings = XDocument.Load(settingsFileName);
}
public void CreateNew()
{
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Settings"));
if (!Directory.Exists(Path.GetDirectoryName(settingsFileName))) Directory.CreateDirectory(Path.GetDirectoryName(settingsFileName));
doc.Save(settingsFileName);
}
public string Read(string key, string defaultValue = "")
{
var data = from item in docSettings.Root.Elements("Setting")
where item.Element("Key").Value == key
select item.Element("Value").Value;
string retVal = "";
foreach (var p in data)
{
Console.WriteLine(p.ToString());
retVal = p.ToString();
}
if (retVal == "")
{
XElement newClient = new XElement("Setting",
new XElement("Key", key),
new XElement("Value", defaultValue)
);
return defaultValue;
}
else
{
return retVal;
}
}
public bool Write(string key, string value)
{
var data = from item in docSettings.Root.Elements("Setting")
where item.Element("Key").Value == key
select item.Element("Value").Value;
if (data.Count() == 0)
{
XElement newClient = new XElement("Setting",
new XElement("Key", key),
new XElement("Value", value)
);
docSettings.Root.Add(newClient);
}
else //Client section exists.
{
var x = docSettings.Descendants("Setting");
foreach (var p in x)
{
if (p.Element("Key").Value == key)
p.Element("Value").Value = value;
}
//x.Value = value;
}
docSettings.Save(settingsFileName);
return true;
}
public void SaveWindowLocation(Form frm, string key = "Window Location")
{
if (frm.WindowState == FormWindowState.Normal)
{
Write(key, frm.Location.X + "," + frm.Location.Y + "," + frm.Size.Width + "," + frm.Size.Height);
}
else
{
string sLoc = Read(key);
if (sLoc != "")
{
string[] s = sLoc.Split(',');
if (s.Length > 3) Write(key, s[0] + "," + s[1] + "," + s[2] + "," + s[3] + "," + frm.WindowState.ToString());
//frm.Location.
}
}
}
public void SetWindowLocation(Form frm, string key = "Window Location")
{
try
{
string sLoc = Read(key);
if (sLoc != "")
{
string[] s = sLoc.Split(',');
frm.Location = new Point(System.Convert.ToInt32(s[0]), System.Convert.ToInt32(s[1]));
frm.Size = new Size(System.Convert.ToInt32(s[2]), System.Convert.ToInt32(s[3]));
if (s.Length == 5) frm.WindowState = (s[4] == "Maximized" ? FormWindowState.Maximized : FormWindowState.Minimized);
}
else
{
// First time, so save something
Write(key, frm.Location.X + "," + frm.Location.Y + "," + frm.Size.Width + "," + frm.Size.Height);
}
}
catch
{
}
}
}
}