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

Contents of /trunk/TSAdminTool/Globals.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 119 - (show annotations) (download)
Sat Jul 14 09:08:55 2012 UTC (9 years, 2 months ago) by william
File size: 48891 byte(s)
+ bulk commit again

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

  ViewVC Help
Powered by ViewVC 1.1.22