1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Text; |
4 |
using System.Reflection; // For path (assembly) |
5 |
using System.IO; // For Path |
6 |
using System.Security; // Security Exception |
7 |
using System.Windows.Forms; // Message Box |
8 |
using System.Text.RegularExpressions; // For validate MAC address |
9 |
using log4net; |
10 |
|
11 |
namespace AnywhereTS |
12 |
{ |
13 |
public static class ATSGlobals |
14 |
{ |
15 |
#region Global Definitions |
16 |
// Version |
17 |
public const string strDatabaseVersion = "1.0.0.4"; // The version string for the ATS database |
18 |
public const string strDatabaseName = "AnywhereTS"; // THe name of the ATS database |
19 |
//[SerializableAttribute] public struct Color; |
20 |
|
21 |
// Registry keys |
22 |
public static string strATSregRoot; // Registry root for AnywhereTS |
23 |
public static string strTFTPD32RegRoot; // Registry root for TFTPD32 |
24 |
public const string strRegDHCPconfig = "DHCP"; // DCHP config in registry |
25 |
public const string strRegTFTPconfig = "TFTP"; // TFTP root directory in registry |
26 |
public const string strRegTFTP_root = "TFTP_root"; // TFTP root directory in registry |
27 |
public const string strRegManagedMode = "ManagedMode"; // Reg key for managed mode. |
28 |
public const string strRegConfigured = "Configured"; // Reg key for indicating of ATS is configured. |
29 |
public const string strRegTerminalServer = "TerminalServer"; // Reg key for terinal server config. |
30 |
public const string strRegRunFirstTime = "RunFirstTime"; // Reg key for first time run. |
31 |
public const string strRegAdminVersion = "AdminVersion"; // Reg key for Admin version. Indicates that Admin app is intalled on this computer and the version of the admin app. |
32 |
|
33 |
// Help |
34 |
public static string helpFile; // The ATS help file |
35 |
|
36 |
// Directories |
37 |
static public string strTFTPdir; // The TFP root directory, including final "\" |
38 |
static public string strExecPath; // The directory for the application exe-file |
39 |
static public string strHelpFilePath; // The path (incl filname) to the application help file |
40 |
|
41 |
|
42 |
// Other global variables |
43 |
static public int managedMode; // 0=Not definied, 1=Managed, 2=Unmanged |
44 |
static public int configured; // 0=ATS not configured, 1=ATS is configured, 2=Configured for control panel only. |
45 |
static public int dhcpConfig; // 0=Use internal DHCP, 1=Other DHCP |
46 |
static public int tftpConfig; // 0=Use internal TFTP, 1=Other TFTP |
47 |
static public int terminalServerConfig; // 0=Terminal server on this computer, 1=No terminal server on this computer. |
48 |
static public int runFirstTime; // 1=The application (or new version of it) is newly installed and the first time run procedures should be run. 0=The application has already been run. |
49 |
|
50 |
static public bool isPro=true; // True = we are running the Pro version. Always true for the open source version. |
51 |
static public bool isEval=true; //True if evalversion of client Non pro feature |
52 |
static public string SelectedGraphicsAdaptersFile; // Name of the file for graphics adapters that corresponds to the users selection of linux kernel |
53 |
static public string SelectedNicAdaptersFile; // Name of the file for network adapters that corresponds to the users selection of linux kernel |
54 |
static public string SelectedSoundAdaptersFile; // Name of the file for sound adapters that corresponds to the users selection of linux kernel |
55 |
static public string ApplicationName; // The name of the application. Used everywhere except for in about box and main window bar. Can be either original or an OEM name. |
56 |
static public string ApplicationTitle = ApplicationName; // The title of the application, used for the main window bar and about box. |
57 |
|
58 |
//File names |
59 |
public const string GraphicsAdaptersA = "Graphics30.ats"; //The file containing the list of all graphics adapters |
60 |
public const string NicAdaptersA = "Nic30.ats"; // The file containing the list of all network adapters |
61 |
public const string SoundAdaptersA = "Sound30.ats"; // The file containing the list of all sound adapters |
62 |
|
63 |
// Client connect time options |
64 |
public static int[,] ScreenResolutions = new int[,] { { 640, 480 }, { 800, 600 }, { 1024, 600 }, { 1024, 768 }, { 1152, 864 }, { 1280, 768 }, { 1280, 800 }, { 1280, 960 }, { 1280, 1024 }, { 1368, 768 }, { 1400, 1050 }, { 1440, 900 }, { 1440, 1050 }, { 1600, 1000 }, { 1600, 1024 }, { 1600, 1200 }, { 1680, 1050 }, { 1792, 1344 }, { 1856, 1392 }, { 1920, 1080 }, { 1920, 1200 }, { 1920, 1440 } }; |
65 |
#endregion |
66 |
#region static ATSGlobals() |
67 |
// Constructor |
68 |
static ATSGlobals() |
69 |
{ |
70 |
Logging.ATSAdminLog.Debug("Entering ATSGlobals()"); |
71 |
ApplicationName = "Anywhere" + "TS"; // The name of the application. Used everywhere except for in about box and main window bar. Can be either original or an OEM name. |
72 |
|
73 |
// Set registry root |
74 |
// This is how we find the OS on run time |
75 |
// on 32 bit OS IntPtr.Size = 4 |
76 |
// on 64 bit OS IntPtr.Size = 8 |
77 |
if (IntPtr.Size == 8) |
78 |
{ // 64 bit OS |
79 |
strATSregRoot = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config"; |
80 |
} |
81 |
else |
82 |
{ // 32 bit OS |
83 |
strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config"; |
84 |
} |
85 |
|
86 |
|
87 |
// Set registry root for TFTPD32 |
88 |
// This is how we find the OS on run time |
89 |
// on 32 bit OS IntPtr.Size = 4 |
90 |
// on 64 bit OS IntPtr.Size = 8 |
91 |
if (IntPtr.Size == 8) |
92 |
{ // 64 bit OS |
93 |
strTFTPD32RegRoot = @"SOFTWARE\Wow6432Node\TFTPD32"; |
94 |
} |
95 |
else |
96 |
{ // 32 bit OS |
97 |
strTFTPD32RegRoot = @"SOFTWARE\TFTPD32"; |
98 |
} |
99 |
|
100 |
|
101 |
helpFile = "AnywhereTS.chm"; // The ATS help file |
102 |
|
103 |
//if (GetATSRegValueInt(strRegRunFirstTime) == 1) |
104 |
//{ // We are runnning this version for the first time, write the needed registry values |
105 |
// CreateRegistryValues(); |
106 |
//} |
107 |
|
108 |
strExecPath = Path.GetDirectoryName(Application.ExecutablePath); |
109 |
|
110 |
strHelpFilePath = strExecPath + @"\" + helpFile; |
111 |
|
112 |
// Get TFTP config |
113 |
tftpConfig = GetATSRegValueInt(strRegTFTPconfig); |
114 |
|
115 |
// Get TFTP root |
116 |
strTFTPdir = GetATSRegValueString(strRegTFTP_root); |
117 |
|
118 |
// Get DHCP config |
119 |
dhcpConfig = GetATSRegValueInt(strRegDHCPconfig); |
120 |
|
121 |
configured = GetATSRegValueInt(strRegConfigured); |
122 |
|
123 |
managedMode = GetATSRegValueInt(strRegManagedMode); |
124 |
|
125 |
runFirstTime = GetATSRegValueInt(strRegRunFirstTime); |
126 |
|
127 |
// Currently only the 3.0 version of the drivers are supported |
128 |
SelectedGraphicsAdaptersFile = GraphicsAdaptersA; |
129 |
SelectedNicAdaptersFile = NicAdaptersA; |
130 |
SelectedSoundAdaptersFile = SoundAdaptersA; |
131 |
|
132 |
ProSupport.strDatabaseServer = GetATSRegValueString(ProSupport.strRegDatabaseServer); |
133 |
ProSupport.strDatabaseInstance = GetATSRegValueString(ProSupport.strRegDatabaseInstance); |
134 |
|
135 |
Logging.ATSAdminLog.Debug("Leaving ATSGlobals()"); |
136 |
|
137 |
} |
138 |
#endregion |
139 |
|
140 |
#region public static string GetATSRegValueString(string regValue) |
141 |
// Get a registry string |
142 |
public static string GetATSRegValueString(string regValue) |
143 |
{ |
144 |
string value = string.Empty; |
145 |
using (log4net.NDC.Push(string.Format("key={0} name={1}", strATSregRoot, regValue))) |
146 |
{ |
147 |
Logging.ATSAdminLog.DebugFormat(@"Getting value from Registry: {0}\{1}", strATSregRoot, regValue); |
148 |
value = GetRegValue(strATSregRoot, regValue); |
149 |
Logging.ATSAdminLog.DebugFormat(@"value={0}", value); |
150 |
} |
151 |
return value; |
152 |
} |
153 |
#endregion |
154 |
|
155 |
#region public static int GetATSRegValueInt(string regValue) |
156 |
// Get a registry integer from the application key |
157 |
public static int GetATSRegValueInt(string regValue) |
158 |
{ |
159 |
int value = -1; |
160 |
using (log4net.NDC.Push(string.Format("key={0} name={1}", strATSregRoot, regValue))) |
161 |
{ |
162 |
Logging.ATSAdminLog.DebugFormat(@"Getting value from Registry: {0}\{1}", strATSregRoot, regValue); |
163 |
value = GetRegValueInt(strATSregRoot, regValue); |
164 |
Logging.ATSAdminLog.DebugFormat(@"value={0}", value); |
165 |
} |
166 |
return value; |
167 |
} |
168 |
#endregion |
169 |
|
170 |
#region public static void SetATSRegValue(string regValue, int value) |
171 |
// Set a registry integer in the the application key |
172 |
public static void SetATSRegValue(string regValue, int value) |
173 |
{ |
174 |
SetRegValue(strATSregRoot, regValue, value); |
175 |
} |
176 |
#endregion |
177 |
|
178 |
#region public static void SetATSRegValue(string regValue, string value) |
179 |
// Set a registry string in the the application key |
180 |
public static void SetATSRegValue(string regValue, string value) |
181 |
{ |
182 |
SetRegValue(strATSregRoot, regValue, value); |
183 |
} |
184 |
#endregion |
185 |
|
186 |
#region public static string GetRegValue(string regKey, string regValue) |
187 |
// Get a registry string |
188 |
public static string GetRegValue(string regKey, string regValue) |
189 |
{ |
190 |
Microsoft.Win32.RegistryKey objRegkey; |
191 |
|
192 |
try |
193 |
{ |
194 |
objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey); |
195 |
} |
196 |
catch (ArgumentNullException ex) |
197 |
{ |
198 |
//MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)"); |
199 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString()))) |
200 |
{ |
201 |
using (log4net.NDC.Push("Installation info missing.")) |
202 |
{ |
203 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
204 |
} |
205 |
} |
206 |
Application.Exit(); |
207 |
return ""; |
208 |
} |
209 |
catch (SecurityException ex) |
210 |
{ |
211 |
//MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: " + regKey + " Value: " + regValue + " Error details:" + ex.Message); |
212 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString()))) |
213 |
{ |
214 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
215 |
{ |
216 |
using (log4net.NDC.Push("Invalid users rights.")) |
217 |
{ |
218 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
219 |
} |
220 |
} |
221 |
} |
222 |
Application.Exit(); |
223 |
return ""; |
224 |
} |
225 |
catch (Exception ex) |
226 |
{ |
227 |
//MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + ex.Message); |
228 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString()))) |
229 |
{ |
230 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
231 |
} |
232 |
Application.Exit(); |
233 |
return ""; |
234 |
} |
235 |
|
236 |
if (objRegkey != null) |
237 |
{ |
238 |
if (objRegkey.GetValue(regValue) != null) |
239 |
{ |
240 |
try |
241 |
{ |
242 |
return objRegkey.GetValue(regValue).ToString(); |
243 |
} |
244 |
catch (Exception e) |
245 |
{ |
246 |
//MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
247 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
248 |
{ |
249 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
250 |
} |
251 |
Application.Exit(); |
252 |
return ""; |
253 |
} |
254 |
} |
255 |
else |
256 |
{ |
257 |
//MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue); |
258 |
using (log4net.NDC.Push(string.Format("key={0}", regKey))) |
259 |
{ |
260 |
using (log4net.NDC.Push("Value is missing")) |
261 |
{ |
262 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
263 |
} |
264 |
} |
265 |
Application.Exit(); |
266 |
return ""; |
267 |
} |
268 |
} |
269 |
else |
270 |
{ |
271 |
//MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root); |
272 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
273 |
{ |
274 |
using (log4net.NDC.Push("Key is missing")) |
275 |
{ |
276 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
277 |
} |
278 |
} |
279 |
Application.Exit(); |
280 |
} |
281 |
return ""; // No value could be retrieved from registry |
282 |
} |
283 |
#endregion |
284 |
|
285 |
#region public static int GetRegValueInt(string regKey, string regValue) |
286 |
// Get a registry integer |
287 |
public static int GetRegValueInt(string regKey, string regValue) |
288 |
{ |
289 |
|
290 |
Microsoft.Win32.RegistryKey objRegkey; |
291 |
|
292 |
try |
293 |
{ |
294 |
objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey); |
295 |
} |
296 |
catch (ArgumentNullException ex) |
297 |
{ |
298 |
//MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)"); |
299 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString()))) |
300 |
{ |
301 |
using (log4net.NDC.Push("Installation info missing.")) |
302 |
{ |
303 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
304 |
} |
305 |
} |
306 |
Application.Exit(); |
307 |
return 0; |
308 |
} |
309 |
catch (SecurityException e) |
310 |
{ |
311 |
//MessageBox.Show("Error when reading from registry. You do not have the necessary permission (22412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
312 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
313 |
{ |
314 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
315 |
{ |
316 |
using (log4net.NDC.Push("Invalid user rights.")) |
317 |
{ |
318 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
319 |
} |
320 |
} |
321 |
} |
322 |
Application.Exit(); |
323 |
return 0; |
324 |
} |
325 |
catch (Exception e) |
326 |
{ |
327 |
//MessageBox.Show("Error when reading from registry (22413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
328 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
329 |
{ |
330 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
331 |
} |
332 |
Application.Exit(); |
333 |
return 0; |
334 |
} |
335 |
|
336 |
if (objRegkey != null) |
337 |
{ |
338 |
if (objRegkey.GetValue(regValue) != null) |
339 |
{ |
340 |
try |
341 |
{ |
342 |
return (int)objRegkey.GetValue(regValue); |
343 |
} |
344 |
catch (Exception e) |
345 |
{ |
346 |
//MessageBox.Show("Error when reading from registry (22414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
347 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
348 |
{ |
349 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
350 |
{ |
351 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
352 |
} |
353 |
} |
354 |
Application.Exit(); |
355 |
return 0; |
356 |
} |
357 |
} |
358 |
else |
359 |
{ |
360 |
//MessageBox.Show("Error when reading from registry (55222). Value missing. Key: " + regKey + " Value: " + regValue); |
361 |
|
362 |
using (log4net.NDC.Push(string.Format("key={0}", regKey))) |
363 |
{ |
364 |
using (log4net.NDC.Push("Value is missing")) |
365 |
{ |
366 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
367 |
} |
368 |
} |
369 |
|
370 |
Application.Exit(); |
371 |
return 0; |
372 |
} |
373 |
} |
374 |
else |
375 |
{ |
376 |
//MessageBox.Show("Error when reading from registry (55221). Key missing. Key: " + regKey + " Value: " + regValue); |
377 |
|
378 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
379 |
{ |
380 |
using (log4net.NDC.Push("Key is missing")) |
381 |
{ |
382 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
383 |
} |
384 |
} |
385 |
|
386 |
Application.Exit(); |
387 |
} |
388 |
return 0; // No value could be retrieved from registry |
389 |
} |
390 |
#endregion |
391 |
|
392 |
#region public static void SetRegValue(string regKey, string regValue, int value) |
393 |
// Set a registry integer |
394 |
public static void SetRegValue(string regKey, string regValue, int value) |
395 |
{ |
396 |
using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value))) |
397 |
{ |
398 |
Logging.ATSAdminLog.DebugFormat(@"Setting value in Registry: {0}\{1}={2}", regKey, regValue, value); |
399 |
Microsoft.Win32.RegistryKey objRegkey; |
400 |
|
401 |
try |
402 |
{ |
403 |
objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey, true); |
404 |
} |
405 |
catch (ArgumentNullException e) |
406 |
{ |
407 |
MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)"); |
408 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
409 |
{ |
410 |
using (log4net.NDC.Push("Installation info missing")) |
411 |
{ |
412 |
Logging.ATSAdminLog.Error("Error when writing to registry."); |
413 |
} |
414 |
} |
415 |
Application.Exit(); |
416 |
return; |
417 |
} |
418 |
catch (SecurityException e) |
419 |
{ |
420 |
MessageBox.Show("Error when writing to registry. You do not have the necessary permission (62412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
421 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
422 |
{ |
423 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
424 |
{ |
425 |
using (log4net.NDC.Push("Invalid user rights")) |
426 |
{ |
427 |
Logging.ATSAdminLog.Error("Error when writing to registry."); |
428 |
} |
429 |
} |
430 |
} |
431 |
Application.Exit(); |
432 |
return; |
433 |
} |
434 |
catch (Exception e) |
435 |
{ |
436 |
MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
437 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
438 |
{ |
439 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
440 |
{ |
441 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
442 |
} |
443 |
} |
444 |
Application.Exit(); |
445 |
return; |
446 |
} |
447 |
|
448 |
if (objRegkey != null) |
449 |
{ |
450 |
try |
451 |
{ |
452 |
objRegkey.SetValue(regValue, value); |
453 |
} |
454 |
catch (Exception e) |
455 |
{ |
456 |
MessageBox.Show("Error when writing to registry (62414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
457 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
458 |
{ |
459 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
460 |
{ |
461 |
Logging.ATSAdminLog.Error("Error when writing to registry."); |
462 |
} |
463 |
} |
464 |
Application.Exit(); |
465 |
} |
466 |
} |
467 |
else |
468 |
{ |
469 |
MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue); |
470 |
|
471 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
472 |
{ |
473 |
using (log4net.NDC.Push("Key is missing")) |
474 |
{ |
475 |
Logging.ATSAdminLog.Error("Error when writing to registry."); |
476 |
} |
477 |
} |
478 |
|
479 |
Application.Exit(); |
480 |
} |
481 |
} |
482 |
} |
483 |
#endregion |
484 |
|
485 |
#region public static void SetRegValue(string regKey, string regValue, string value) |
486 |
// Set a registry string |
487 |
public static void SetRegValue(string regKey, string regValue, string value) |
488 |
{ |
489 |
using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value))) |
490 |
{ |
491 |
Logging.ATSAdminLog.DebugFormat(@"Setting value in Registry: {0}\{1}={2}", regKey, regValue, value); |
492 |
Microsoft.Win32.RegistryKey objRegkey; |
493 |
|
494 |
try |
495 |
{ |
496 |
objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey, true); |
497 |
} |
498 |
catch (ArgumentNullException e) |
499 |
{ |
500 |
MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)"); |
501 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
502 |
{ |
503 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
504 |
{ |
505 |
Logging.ATSAdminLog.Error("Error when writing to registry."); |
506 |
} |
507 |
} |
508 |
Application.Exit(); |
509 |
return; |
510 |
} |
511 |
catch (SecurityException e) |
512 |
{ |
513 |
MessageBox.Show("Error when writing to registry. You do not have the necessary permission (62412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
514 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
515 |
{ |
516 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
517 |
{ |
518 |
using (log4net.NDC.Push("Invalid user rights")) |
519 |
{ |
520 |
Logging.ATSAdminLog.Error("Error when writing to registry."); |
521 |
} |
522 |
} |
523 |
} |
524 |
Application.Exit(); |
525 |
return; |
526 |
} |
527 |
catch (Exception e) |
528 |
{ |
529 |
MessageBox.Show("Error when reading from registry (62413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
530 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
531 |
{ |
532 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
533 |
{ |
534 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
535 |
} |
536 |
} |
537 |
Application.Exit(); |
538 |
return; |
539 |
} |
540 |
|
541 |
if (objRegkey != null) |
542 |
{ |
543 |
try |
544 |
{ |
545 |
objRegkey.SetValue(regValue, value); |
546 |
} |
547 |
catch (Exception e) |
548 |
{ |
549 |
MessageBox.Show("Error when writing to registry (62414). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
550 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
551 |
{ |
552 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
553 |
{ |
554 |
Logging.ATSAdminLog.Error("Error when writing to registry."); |
555 |
} |
556 |
} |
557 |
Application.Exit(); |
558 |
} |
559 |
} |
560 |
else |
561 |
{ |
562 |
MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue); |
563 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
564 |
{ |
565 |
using (log4net.NDC.Push("Key is missing")) |
566 |
{ |
567 |
Logging.ATSAdminLog.Error("Error when writing to registry."); |
568 |
} |
569 |
} |
570 |
|
571 |
Application.Exit(); |
572 |
} |
573 |
} |
574 |
} |
575 |
#endregion |
576 |
|
577 |
#region public static bool RegValueExists(string regKey, string regValue) |
578 |
// Check if a registry int value exists |
579 |
public static bool RegValueExists(string regKey, string regValue) |
580 |
{ |
581 |
Microsoft.Win32.RegistryKey objRegkey; |
582 |
|
583 |
try |
584 |
{ |
585 |
objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey); |
586 |
} |
587 |
catch (ArgumentNullException e) |
588 |
{ |
589 |
//MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (29411)"); |
590 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
591 |
{ |
592 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
593 |
{ |
594 |
using (log4net.NDC.Push("Installation info missing")) |
595 |
{ |
596 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
597 |
} |
598 |
} |
599 |
} |
600 |
Application.Exit(); |
601 |
return false; |
602 |
} |
603 |
catch (SecurityException e) |
604 |
{ |
605 |
//MessageBox.Show("Error when reading from registry. You do not have the necessary permission (29412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
606 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
607 |
{ |
608 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
609 |
{ |
610 |
using (log4net.NDC.Push("Invalid user rights")) |
611 |
{ |
612 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
613 |
} |
614 |
} |
615 |
} |
616 |
Application.Exit(); |
617 |
return false; |
618 |
} |
619 |
catch (Exception e) |
620 |
{ |
621 |
//MessageBox.Show("Error when reading from registry (29413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
622 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
623 |
{ |
624 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
625 |
{ |
626 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
627 |
} |
628 |
} |
629 |
Application.Exit(); |
630 |
return false; |
631 |
} |
632 |
|
633 |
if (objRegkey != null) |
634 |
{ |
635 |
if (objRegkey.GetValue(regValue) != null) |
636 |
{ |
637 |
return true; // We could retrive the value |
638 |
} |
639 |
else |
640 |
{ |
641 |
return false; // No value found |
642 |
} |
643 |
} |
644 |
else |
645 |
{ |
646 |
//MessageBox.Show("Error when reading from registry (59221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root); |
647 |
|
648 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
649 |
{ |
650 |
using (log4net.NDC.Push("Key is missing")) |
651 |
{ |
652 |
Logging.ATSAdminLog.Error("Error when reading from registry."); |
653 |
} |
654 |
} |
655 |
|
656 |
Application.Exit(); |
657 |
} |
658 |
return false; // No value could be retrieved from registry |
659 |
} |
660 |
#endregion |
661 |
|
662 |
#region public static string IsValidMAC(string macAddress) |
663 |
// Validate MAC a MAC address. |
664 |
public static string IsValidMAC(string macAddress) |
665 |
{ |
666 |
macAddress = macAddress.Replace(":", ""); |
667 |
macAddress = macAddress.ToUpper(); |
668 |
string result = ""; |
669 |
Regex rx = new Regex("([0-9a-fA-F][0-9a-fA-F]-){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase); |
670 |
Match m = rx.Match(macAddress); |
671 |
result = m.Groups[0].Value; |
672 |
if (result.Length == 17) |
673 |
{ |
674 |
return result; |
675 |
} |
676 |
else |
677 |
{ |
678 |
rx = new Regex("([0-9a-fA-F][0-9a-fA-F]){5}([0-9a-fA-F][0-9a-fA-F])", RegexOptions.IgnoreCase); |
679 |
Match m2 = rx.Match(macAddress); |
680 |
result = m2.Groups[0].Value; |
681 |
if (result.Length == 12) |
682 |
{ |
683 |
return result; |
684 |
} |
685 |
return result; |
686 |
} |
687 |
} |
688 |
#endregion |
689 |
|
690 |
#region public static string GetMacFromATSname(string name) |
691 |
// Try to extract a mac address from an ATS client name in the format 'ATSxxxxxxxxxxxx', where xxxxxxxxxx is a mac address. |
692 |
public static string GetMacFromATSname(string name) |
693 |
{ |
694 |
string strMac; |
695 |
if (name.Length != 15) |
696 |
{ |
697 |
return ""; // Not an ATS name |
698 |
} |
699 |
if (!name.StartsWith("ATS")) |
700 |
{ |
701 |
return ""; // Not an ATS name |
702 |
} |
703 |
strMac = ATSGlobals.IsValidMAC(name.Substring(3)); |
704 |
if (strMac == "") |
705 |
{ |
706 |
return ""; // Not an ATS name |
707 |
} |
708 |
// We have a MAC address |
709 |
return strMac; |
710 |
} |
711 |
#endregion |
712 |
|
713 |
#region public static void CreateRegistryValues() |
714 |
// Create all needed registry values |
715 |
public static void CreateRegistryValues() |
716 |
{ |
717 |
// Create registry values for ATS Admin Application |
718 |
CreateRegistryValue(strATSregRoot, "CheckUpdate", 1); |
719 |
CreateRegistryValue(strATSregRoot, strRegConfigured, 0); |
720 |
CreateRegistryValue(strATSregRoot, ProSupport.strRegAnywhereTSServer, ""); |
721 |
#if HAVE_FRMCONFIGMODE |
722 |
CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseDir, ""); |
723 |
CreateRegistryValue(strATSregRoot, ProSupport.strRegDestDir, ""); |
724 |
#endif |
725 |
CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseServer, ProSupport.strDatabaseServer); |
726 |
CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseInstance, ProSupport.strDatabaseInstance); |
727 |
|
728 |
|
729 |
CreateRegistryValue(strATSregRoot, strRegDHCPconfig , 0); |
730 |
CreateRegistryValue(strATSregRoot, strRegManagedMode, 1); |
731 |
CreateRegistryValue(strATSregRoot, strRegTerminalServer, 0); |
732 |
CreateRegistryValue(strATSregRoot, strRegTFTPconfig, 0); |
733 |
CreateRegistryValue(strATSregRoot, strRegTFTP_root,""); |
734 |
|
735 |
// Check if the admin app is intalled on this computer and if so, add reg values for TFTP 32 |
736 |
if (RegValueExists(strATSregRoot, strRegAdminVersion)) |
737 |
{ // The admin app is installed, define keys for TFTP32. These should not be installed for the control panel. |
738 |
// Create registry values for TFTPD32 |
739 |
CreateRegistryValue(strTFTPD32RegRoot, "BaseDirectory", ""); |
740 |
CreateRegistryValue(strTFTPD32RegRoot, "Beep", 0); |
741 |
CreateRegistryValue(strTFTPD32RegRoot, "DirText", 0); |
742 |
CreateRegistryValue(strTFTPD32RegRoot, "Hide", 0); |
743 |
CreateRegistryValue(strTFTPD32RegRoot, "LastWindowPos", "60 49 860 642 "); |
744 |
CreateRegistryValue(strTFTPD32RegRoot, "LocalIP", ""); |
745 |
CreateRegistryValue(strTFTPD32RegRoot, "MaxRetransmit", 6); |
746 |
CreateRegistryValue(strTFTPD32RegRoot, "Negociate", 1); |
747 |
CreateRegistryValue(strTFTPD32RegRoot, "PXECompatibility", 0); |
748 |
CreateRegistryValue(strTFTPD32RegRoot, "SaveSyslogFile", ""); |
749 |
CreateRegistryValue(strTFTPD32RegRoot, "SecurityLevel", 1); |
750 |
CreateRegistryValue(strTFTPD32RegRoot, "Services", 2); |
751 |
CreateRegistryValue(strTFTPD32RegRoot, "ShowProgressBar", 0); |
752 |
CreateRegistryValue(strTFTPD32RegRoot, "TftpLogFile", ""); |
753 |
CreateRegistryValue(strTFTPD32RegRoot, "TftpPort", 69); |
754 |
CreateRegistryValue(strTFTPD32RegRoot, "Timeout", 3); |
755 |
CreateRegistryValue(strTFTPD32RegRoot, "UnixStrings", 1); |
756 |
CreateRegistryValue(strTFTPD32RegRoot, "VirtualRoot", 1); |
757 |
CreateRegistryValue(strTFTPD32RegRoot, "WinSize", 0); |
758 |
|
759 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber1", 0); |
760 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber2", 0); |
761 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue1", ""); |
762 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue2", ""); |
763 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "BootFile", "client.zpxe"); |
764 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DNS", 16834314); |
765 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DomainName", ""); |
766 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Gateway", 16834314); |
767 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "IP_Pool", 335601418); |
768 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Mask", 65535); |
769 |
CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "PoolSize", 200); |
770 |
} |
771 |
} |
772 |
#endregion |
773 |
|
774 |
#region public static void CreateRegistryValue(string regKey, string regValue, int value) |
775 |
// If a registry key does not exist, create it with the given default value |
776 |
public static void CreateRegistryValue(string regKey, string regValue, int value) |
777 |
{ |
778 |
using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}",regKey, regValue, value))) |
779 |
{ |
780 |
Logging.ATSAdminLog.DebugFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value); |
781 |
Microsoft.Win32.RegistryKey objRegkey; |
782 |
|
783 |
try |
784 |
{ |
785 |
objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey); |
786 |
} |
787 |
catch (ArgumentNullException e) |
788 |
{ |
789 |
MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)"); |
790 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
791 |
{ |
792 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
793 |
{ |
794 |
using (log4net.NDC.Push("Installation info missing")) |
795 |
{ |
796 |
Logging.ATSAdminLog.Error("Failed to create value in registry"); |
797 |
} |
798 |
} |
799 |
} |
800 |
Application.Exit(); |
801 |
return; |
802 |
} |
803 |
catch (SecurityException e) |
804 |
{ |
805 |
MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
806 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
807 |
{ |
808 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
809 |
{ |
810 |
using (log4net.NDC.Push("Invalid user rights")) |
811 |
{ |
812 |
Logging.ATSAdminLog.Error("Failed to create value in registry"); |
813 |
} |
814 |
} |
815 |
} |
816 |
Application.Exit(); |
817 |
return; |
818 |
} |
819 |
catch (Exception e) |
820 |
{ |
821 |
MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
822 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
823 |
{ |
824 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
825 |
{ |
826 |
Logging.ATSAdminLog.Error("Failed to create value in registry"); |
827 |
|
828 |
} |
829 |
} |
830 |
Application.Exit(); |
831 |
return; |
832 |
} |
833 |
|
834 |
if (objRegkey != null) |
835 |
{ |
836 |
if (objRegkey.GetValue(regValue) == null) |
837 |
{ // No reg value, create one |
838 |
SetRegValue(regKey, regValue, value); |
839 |
} |
840 |
} |
841 |
else |
842 |
{ |
843 |
MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root); |
844 |
|
845 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
846 |
{ |
847 |
Logging.ATSAdminLog.Error("Failed to create value in registry"); |
848 |
} |
849 |
|
850 |
Application.Exit(); |
851 |
} |
852 |
} |
853 |
} |
854 |
#endregion |
855 |
|
856 |
#region public static void CreateRegistryValue(string regKey, string regValue, string value) |
857 |
// If a registry key does not exist, create it with the given default value |
858 |
public static void CreateRegistryValue(string regKey, string regValue, string value) |
859 |
{ |
860 |
using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value))) |
861 |
{ |
862 |
Logging.ATSAdminLog.DebugFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value); |
863 |
Microsoft.Win32.RegistryKey objRegkey; |
864 |
|
865 |
try |
866 |
{ |
867 |
objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey); |
868 |
} |
869 |
catch (ArgumentNullException e) |
870 |
{ |
871 |
MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)"); |
872 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
873 |
{ |
874 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
875 |
{ |
876 |
using (log4net.NDC.Push("Installation info missing")) |
877 |
{ |
878 |
Logging.ATSAdminLog.Error("Failed to create value in registry"); |
879 |
} |
880 |
} |
881 |
} |
882 |
Application.Exit(); |
883 |
return; |
884 |
} |
885 |
catch (SecurityException e) |
886 |
{ |
887 |
MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
888 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
889 |
{ |
890 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
891 |
{ |
892 |
using (log4net.NDC.Push("Invalid users rights")) |
893 |
{ |
894 |
Logging.ATSAdminLog.Error("Failed to create value in registry"); |
895 |
} |
896 |
} |
897 |
} |
898 |
Application.Exit(); |
899 |
return; |
900 |
} |
901 |
catch (Exception e) |
902 |
{ |
903 |
MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message); |
904 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
905 |
{ |
906 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
907 |
{ |
908 |
Logging.ATSAdminLog.Error("Failed to create value in registry"); |
909 |
} |
910 |
} |
911 |
Application.Exit(); |
912 |
return; |
913 |
} |
914 |
|
915 |
if (objRegkey != null) |
916 |
{ |
917 |
if (objRegkey.GetValue(regValue) == null) |
918 |
{ // No reg value, create one |
919 |
SetRegValue(regKey, regValue, value); |
920 |
} |
921 |
} |
922 |
else |
923 |
{ |
924 |
MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root); |
925 |
|
926 |
using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue))) |
927 |
{ |
928 |
Logging.ATSAdminLog.Error("Failed to create value in registry"); |
929 |
} |
930 |
|
931 |
Application.Exit(); |
932 |
} |
933 |
} |
934 |
} |
935 |
#endregion |
936 |
|
937 |
#region public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers) |
938 |
// Extract two integers from a string, separated by a character |
939 |
// Result: True = extraction ok, false = invalid string. |
940 |
// twointegers: String with two integers, separated by a character |
941 |
// separator: the character separating the strings |
942 |
// value1: (out) The first integer in the string |
943 |
// value2: (out) The second integer in the string |
944 |
public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers) |
945 |
{ |
946 |
string[] parts; |
947 |
parsed_integers = new int[0]; |
948 |
parts = integers.Split(separator); |
949 |
|
950 |
List<int> t_ints = new List<int>(); |
951 |
|
952 |
foreach (string t in parts) |
953 |
{ |
954 |
t_ints.Add(Convert.ToInt32(t)); |
955 |
} |
956 |
parsed_integers = t_ints.ToArray(); |
957 |
return true; |
958 |
} |
959 |
#endregion |
960 |
} // Class ATSGlobals |
961 |
} // Namespace Anywhere TS |