forked from sergiisyrovatchenko/SQLIndexManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
155 lines (127 loc) · 4.68 KB
/
Copy pathSettings.cs
File metadata and controls
155 lines (127 loc) · 4.68 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
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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Serialization;
namespace SQLIndexManager {
public class Settings {
private static GlobalSettings _current;
private static Host _activeHost;
private static readonly Destructor _finalise = new Destructor();
public static bool IgnoreFileSetting = false;
public static Host ActiveHost {
get => _activeHost;
set {
_activeHost = value;
if (_activeHost == null)
return;
Host oldHost = _current.Hosts.FirstOrDefault(_ => string.Equals(_.Server, _activeHost.Server, StringComparison.CurrentCultureIgnoreCase));
if (oldHost != null) {
value.Databases = oldHost.Databases;
}
_current.Hosts.Remove(oldHost);
_current.Hosts.Insert(0, _activeHost);
}
}
public static ServerInfo ServerInfo => _activeHost.ServerInfo;
public static List<Host> Hosts => Instance.Hosts;
public static List<string> NetworkHosts {
get => Instance.NetworkHosts;
set => Instance.NetworkHosts = value;
}
public static Options Options {
get => Instance.Options;
set => Instance.Options = value;
}
private static GlobalSettings Instance {
get {
if (_current == null) {
_current = new GlobalSettings();
if (!IgnoreFileSetting)
Load();
}
return _current;
}
}
private sealed class Destructor {
~Destructor() {
if (!IgnoreFileSetting)
Save();
}
}
public static string ExeName => System.Diagnostics.Process.GetCurrentProcess().ProcessName;
public static string ExePath => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
public static readonly string LayoutFileName = $"{ExePath}\\{ExeName}.layout";
public static readonly string SettingFileName = $"{ExePath}\\{ExeName}.cfg";
public static readonly string LogFileName = $"{ExePath}\\{ExeName}.log";
public static void Save() {
if (File.Exists(SettingFileName)) {
try {
File.Delete(SettingFileName);
}
catch { }
}
XmlSerializer serializer = new XmlSerializer(typeof(GlobalSettings));
try {
using (FileStream writer = File.OpenWrite(SettingFileName)) {
_current.Hosts.RemoveAll(s => !s.IsUserConnection);
_current.Hosts.ForEach(s => {
if (s.AuthType == AuthTypes.SqlServer && s.Password != null) {
s.Password = s.SavePassword
? AES.Encrypt(s.Password)
: null;
}
});
serializer.Serialize(writer, _current);
}
}
catch {
Output.Current.Add("Failed to save settings");
}
}
private static void Load() {
if (File.Exists(SettingFileName)) {
XmlSerializer serializer = new XmlSerializer(typeof(GlobalSettings));
try {
using (StreamReader reader = File.OpenText(SettingFileName)) {
_current = (GlobalSettings)serializer.Deserialize(reader);
_current.Hosts.RemoveAll(_ => _.Server == null);
_current.Hosts.ForEach(s => {
s.IsUserConnection = true;
if (s.AuthType == AuthTypes.SqlServer && !string.IsNullOrEmpty(s.Password)) {
s.Password = AES.Decrypt(s.Password);
s.SavePassword = true;
}
});
}
}
catch {
Output.Current.Add("Failed to load settings");
}
}
RegistryView registryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView)) {
RegistryKey instanceKey = null;
try {
instanceKey = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL", false);
}
catch {
Output.Current.Add("Failed to read registry");
}
if (instanceKey != null) {
foreach (string instanceName in instanceKey.GetValueNames()) {
string host = (instanceName == "MSSQLSERVER") ? Environment.MachineName : $"{Environment.MachineName}\\{instanceName}";
if (!_current.Hosts.Exists(_ => string.Equals(_.Server, host, StringComparison.CurrentCultureIgnoreCase))) {
_current.Hosts.Add(new Host() { Server = host });
}
}
}
}
if (_current.Hosts.Count == 0) {
_current.Hosts.Add(new Host() { Server = Environment.MachineName });
}
}
}
}