/[AnywhereTS-MSSQL]/trunk/TSAdminTool/Globals.cs
ViewVC logotype

Contents of /trunk/TSAdminTool/Globals.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 113 - (show annotations) (download)
Sat Jul 14 07:18:30 2012 UTC (7 years, 7 months ago) by william
File size: 46933 byte(s)
remove all calls to Logging.Initialize() outsite of the Logging class

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

  ViewVC Help
Powered by ViewVC 1.1.22