ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/AnywhereTS-MSSQL/trunk/TSAdminTool/Globals.cs
Revision: 93
Committed: Sat Jul 14 02:35:50 2012 UTC (11 years, 2 months ago) by william
File size: 46526 byte(s)
Log Message:
+ update to .net 3.5 (so we have linq and extension support)

File Contents

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