-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileHelper.cs
More file actions
98 lines (84 loc) · 2.73 KB
/
ProfileHelper.cs
File metadata and controls
98 lines (84 loc) · 2.73 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
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;
using ConnectionStringManager.Properties;
namespace ConnectionStringManager
{
public class ProfileHelper
{
private List<SavedConnection> _connections;
public ReadOnlyCollection<SavedConnection> SavedConnections { get { return _connections.AsReadOnly(); } }
public ProfileHelper()
{
Load();
}
private void Load()
{
try
{
if (Settings.Default["SavedConnections"] == null)
{
_connections = new List<SavedConnection>();
Save();
}
else
{
_connections = Deserialize(Settings.Default["SavedConnections"].ToString());
}
}
catch
{
_connections = new List<SavedConnection>();
}
}
public void Replace(IEnumerable<SavedConnection> connections)
{
_connections.Clear();
_connections.AddRange(connections);
Save();
}
private void Save()
{
Settings.Default["SavedConnections"] = Serialize(_connections);
Settings.Default.Save();
}
public void SaveNewConnection(ConnectionStringEntry entry)
{
string connKey = string.Format("{0}_{1}", entry.ProjectName, entry.ConnectionName);
SavedConnection newConn = new SavedConnection()
{
ConnectionString = entry.ConnectionString,
Name = connKey
};
SaveNewConnection(newConn);
}
public void SaveNewConnection(SavedConnection newConn)
{
_connections.Add(newConn);
Save();
}
public void RemoveConnection(SavedConnection connection)
{
_connections.Remove(connection);
Save();
}
private static List<SavedConnection> Deserialize(string xml)
{
XmlSerializer xs = new XmlSerializer(typeof(List<SavedConnection>));
StringReader sr = new StringReader(xml);
List<SavedConnection> results = (List<SavedConnection>)xs.Deserialize(sr);
return results;
}
private static string Serialize(List<SavedConnection> obj)
{
XmlSerializer xs = new XmlSerializer(typeof(List<SavedConnection>));
StringWriter sw = new StringWriter();
xs.Serialize(sw, obj);
sw.Flush();
string result = sw.ToString();
sw.Close();
return result;
}
}
}