/
AppSettings.cs
108 lines (93 loc) · 2.98 KB
/
AppSettings.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace DetecTOR
{
public enum FormatType
{
IPList = 0,
IPAddInfo = 1
}
[Serializable]
public class AppSettings
{
public string DataUrl { get; set; }
public FormatType DataFormat { get; set; }
public string IPColumn { get; set; }
public string FieldSeparator { get; set; }
public string FlagColumn { get; set; }
public string TrueValue { get; set; }
public string FalseValue { get; set; }
public bool LoadUpdate { get; set; }
public bool AllTorData { get; set; }
public AppSettings()
{
}
}
public class AppSettingsManager
{
private string fileName = "";
public string ConfigError { get; private set; }
public AppSettings Settings = new AppSettings();
public AppSettingsManager(string filename)
{
fileName = filename;
Settings.DataUrl = "https://check.torproject.org/exit-addresses";
Settings.DataFormat = FormatType.IPList;
Settings.FlagColumn = "Flag -";
Settings.IPColumn = "IP Address";
Settings.FieldSeparator=",";
Settings.TrueValue = "1";
Settings.FalseValue = "0";
Settings.LoadUpdate = false;
Settings.AllTorData = false;
}
public bool SaveConfig()
{
FileStream fs = null;
try
{
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
}
catch (Exception ex)
{
ConfigError = ex.Message;
return false;
}
XmlSerializer formatter = new XmlSerializer(typeof(AppSettings));
try
{
fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
formatter.Serialize(fs, Settings);
}
catch (Exception ex)
{
ConfigError = ex.Message;
return false;
}
fs.Close();
return true;
}
public bool LoadConfig()
{
FileStream fs = null;
XmlSerializer formatter = new XmlSerializer(typeof (AppSettings));
if (!File.Exists(fileName)) return true;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
Settings = (AppSettings)formatter.Deserialize(fs);
}
catch (Exception ex)
{
if (fs != null) fs.Close();
ConfigError = ex.Message;
return false;
}
fs.Close();
return true;
}
}
}