-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclsConfig.cs
More file actions
232 lines (201 loc) · 8.26 KB
/
clsConfig.cs
File metadata and controls
232 lines (201 loc) · 8.26 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace QA_Tools
{
class ClsIniFile
{
public static int capacity = 512;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section, string key,
string defaultValue, StringBuilder value, int size, string filePath);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string section, string key, string defaultValue,
[In, Out] char[] value, int size, string filePath);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetPrivateProfileSection(string section, IntPtr keyValue,
int size, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool WritePrivateProfileString(string section, string key,
string value, string filePath);
public static string[] ReadSections(string filePath)
{
// first line will not recognize if ini file is saved in UTF-8 with BOM
while (true)
{
char[] chars = new char[capacity];
int size = GetPrivateProfileString(null, null, "", chars, capacity, filePath);
if (size == 0)
{
return null;
}
if (size < capacity - 2)
{
string result = new String(chars, 0, size);
string[] sections = result.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
return sections;
}
capacity *= 2;
}
}
public static string[] ReadKeys(string section, string filePath)
{
// first line will not recognize if ini file is saved in UTF-8 with BOM
while (true)
{
char[] chars = new char[capacity];
int size = GetPrivateProfileString(section, null, "", chars, capacity, filePath);
if (size == 0)
{
return null;
}
if (size < capacity - 2)
{
string result = new String(chars, 0, size);
string[] keys = result.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
return keys;
}
capacity *= 2;
}
}
public static string ReadValue(string section, string key, string filePath, string defaultValue = "")
{
var value = new StringBuilder(capacity);
GetPrivateProfileString(section, key, defaultValue, value, value.Capacity, filePath);
return value.ToString();
}
}
public class ClsConfig
{
private string pSection, pKey, pValue;
private Dictionary<string, Dictionary<string, string[]>> oConfigList = new Dictionary<string, Dictionary<string, string[]>>();
private static string configFileName;
public string RootFolder { get; set; }
public bool ReadIni()
{
var fName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
fName = Path.ChangeExtension(fName, ".ini");
if (!File.Exists(fName))
{
MessageBox.Show("File does not exists: " + fName);
return true;
}
try
{
string[] Sections = ClsIniFile.ReadSections(fName);
foreach (string Section in Sections)
{
if (Section.ToUpper() == "ROOT")
{
}
else
{
pSection = Section;
string[] KeyList = ClsIniFile.ReadKeys(Section, fName);
//Create list for each Section
Dictionary<string, string[]> oDetailList = new Dictionary<string, string[]>();
foreach (string Key in KeyList)
{
pKey = Key;
string Value = ClsIniFile.ReadValue(Section, Key, fName);
string[] arrDetails = Value.Split(',');
pValue = Value;
oDetailList.Add(Key, arrDetails);
}
oConfigList.Add(Section, oDetailList);
}
}
return false;
}
catch (Exception e)
{
Log(e.Message);
throw new Exception(@"[There is an issue read .ini file]: Section: " + pSection + ", Key: " + pKey + ", Value:" + pValue);
}
}
public Dictionary<string, string[]> GetConfig(char RecordType)
{
return oConfigList[RecordType + "Record"];
}
public string GetRequirement(string key)
{
var v = oConfigList.Keys;
return "NotImplemented";
}
public static void Log(string logMessage)
{
string fn = Path.ChangeExtension(Application.ExecutablePath, ".log");
using (StreamWriter w = File.AppendText(fn))
w.WriteLine(DateTime.Now.ToString("yyMMdd HHmmss") + $": {logMessage}");
}
static public string GetKey(string key, string defaultValue = "")
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
if (config.AppSettings.Settings[key] == null)
{
config.AppSettings.Settings.Add(key, defaultValue);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Properties.Settings.Default.Reload();
}
return config.AppSettings.Settings[key].Value;
}
static public void SetKey(string key, string value)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
if (config.AppSettings.Settings[key] == null)
config.AppSettings.Settings.Add(key, value);
config.AppSettings.Settings[key].Value = value;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Properties.Settings.Default.Reload();
}
catch (Exception e)
{ ClsConfig.Log("SetKey: " + key + ": " + value + ": " + e.Message); }
}
static public string[] GetApis()
{
try
{
List<string> result = new List<string>();
Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
var api = config.AppSettings.Settings.AllKeys.Where(x => x.Contains("Api"));
foreach (string el in api)
{
string URI = config.AppSettings.Settings[el].Value;
if (URI != "")
result.Add(URI);
}
return result.ToArray();
}
catch (Exception e)
{
ClsConfig.Log("GetApis: " + e.Message);
return null;
}
}
static public string[] GetConnections()
{
List<string> connList = new List<string>();
foreach (ConnectionStringSettings c in ConfigurationManager.ConnectionStrings)
{
if (c.Name.Contains("LocalSQLServer")) continue;
connList.Add(c.Name);
}
return connList.ToArray();
}
static public string GetConnectionString(string key)
{
return ConfigurationManager.ConnectionStrings[key].ConnectionString;
}
}
}