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

Annotation of /trunk/TSAdminTool/Globals.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 94 - (hide annotations) (download)
Sat Jul 14 02:52:23 2012 UTC (9 years, 2 months ago) by william
File size: 46655 byte(s)
+ add lots of debug logging with proper context

1 william 4 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 william 47 using log4net;
10 william 4
11     namespace AnywhereTS
12     {
13     public static class ATSGlobals
14     {
15 william 93 #region Global Definitions
16 william 4 // Version
17 william 77 public const string strDatabaseVersion = "1.0.0.4"; // The version string for the ATS database
18 william 81 public const string strDatabaseName = "AnywhereTS"; // THe name of the ATS database
19 william 4 //[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 william 93 #endregion
66     #region static ATSGlobals()
67 william 4 // Constructor
68     static ATSGlobals()
69     {
70 william 94 Logging.ATSAdminLog.Debug("Entering ATSGlobals()");
71 william 4 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 william 94 Logging.ATSAdminLog.Debug("Leaving ATSGlobals()");
132 william 4
133     }
134 william 93 #endregion
135 william 4
136 william 93 #region public static string GetATSRegValueString(string regValue)
137 william 4 // Get a registry string
138     public static string GetATSRegValueString(string regValue)
139     {
140     return GetRegValue(strATSregRoot, regValue);
141     }
142 william 93 #endregion
143 william 4
144 william 93 #region public static int GetATSRegValueInt(string regValue)
145 william 4 // Get a registry integer from the application key
146     public static int GetATSRegValueInt(string regValue)
147     {
148     return GetRegValueInt(strATSregRoot, regValue);
149     }
150 william 93 #endregion
151 william 4
152 william 93 #region public static void SetATSRegValue(string regValue, int value)
153 william 4 // 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 william 93 #endregion
159 william 4
160 william 93 #region public static void SetATSRegValue(string regValue, string value)
161 william 4 // 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 william 93 #endregion
167 william 4
168 william 93 #region public static string GetRegValue(string regKey, string regValue)
169 william 4 // 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 william 46 catch (ArgumentNullException ex)
179 william 4 {
180     MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
181 william 46 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 william 4 Application.Exit();
189     return "";
190     }
191 william 46 catch (SecurityException ex)
192 william 4 {
193 william 46 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 william 4 Application.Exit();
205     return "";
206     }
207 william 46 catch (Exception ex)
208 william 4 {
209 william 46 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 william 4 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 william 46 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 william 4 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 william 93 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
241 william 46 {
242     using (log4net.NDC.Push("Value is missing"))
243     {
244     Logging.ATSAdminLog.Error("Error when reading from registry.");
245     }
246     }
247 william 4 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 william 46 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 william 4 Application.Exit();
262     }
263     return ""; // No value could be retrieved from registry
264     }
265 william 93 #endregion
266 william 4
267 william 93 #region public static int GetRegValueInt(string regKey, string regValue)
268 william 4 // 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 william 46 catch (ArgumentNullException ex)
278 william 4 {
279     MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (22411)");
280 william 46 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 william 4 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 william 46 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 william 4 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 william 46 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 william 4 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 william 46 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 william 4 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 william 47
343 william 93 using (log4net.NDC.Push(string.Format("key={0}", regKey)))
344 william 46 {
345     using (log4net.NDC.Push("Value is missing"))
346     {
347     Logging.ATSAdminLog.Error("Error when reading from registry.");
348     }
349     }
350 william 47
351 william 4 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 william 47
359 william 46 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 william 47
367 william 4 Application.Exit();
368     }
369     return 0; // No value could be retrieved from registry
370     }
371 william 93 #endregion
372 william 4
373 william 93 #region public static void SetRegValue(string regKey, string regValue, int value)
374 william 4 // 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 william 46 catch (ArgumentNullException e)
384 william 4 {
385     MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)");
386 william 46 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 william 4 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 william 46 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 william 4 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 william 46 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 william 4 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 william 46 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 william 4 Application.Exit();
443     }
444     }
445     else
446     {
447     MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue);
448 william 46
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 william 4 Application.Exit();
458     }
459     }
460 william 93 #endregion
461 william 4
462 william 93 #region public static void SetRegValue(string regKey, string regValue, string value)
463 william 4 // 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 william 46 catch (ArgumentNullException e)
473 william 4 {
474     MessageBox.Show("Error when writing to registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (62411)");
475 william 46 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 william 4 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 william 46 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 william 4 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 william 46 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 william 4 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 william 46 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 william 4 Application.Exit();
532     }
533     }
534     else
535     {
536     MessageBox.Show("Error when writing to registry (65222). Key missing. Key: " + regKey + " Value: " + regValue);
537 william 47 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
538 william 46 {
539     using (log4net.NDC.Push("Key is missing"))
540     {
541     Logging.ATSAdminLog.Error("Error when writing to registry.");
542     }
543     }
544 william 47
545 william 4 Application.Exit();
546     }
547     }
548 william 93 #endregion
549 william 4
550 william 93 #region public static bool RegValueExists(string regKey, string regValue)
551 william 4 // 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 william 47 catch (ArgumentNullException e)
561 william 4 {
562     MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (29411)");
563 william 46 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 william 4 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 william 46 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 william 4 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 william 46 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 william 4 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 william 47
621 william 46 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 william 47
629 william 4 Application.Exit();
630     }
631     return false; // No value could be retrieved from registry
632     }
633 william 93 #endregion
634 william 4
635 william 93 #region public static string IsValidMAC(string macAddress)
636 william 4 // 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 william 93 #endregion
662 william 4
663 william 93 #region public static string GetMacFromATSname(string name)
664 william 4 // 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 william 93 #endregion
685    
686     #region public static void CreateRegistryValues()
687 william 4 // 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.strRegDatabaseDir, "");
694     CreateRegistryValue(strATSregRoot, ProSupport.strRegDatabaseServer, "");
695     CreateRegistryValue(strATSregRoot, ProSupport.strRegDestDir, "");
696     CreateRegistryValue(strATSregRoot, strRegDHCPconfig , 0);
697     CreateRegistryValue(strATSregRoot, strRegManagedMode, 1);
698     CreateRegistryValue(strATSregRoot, strRegTerminalServer, 0);
699     CreateRegistryValue(strATSregRoot, strRegTFTPconfig, 0);
700     CreateRegistryValue(strATSregRoot, strRegTFTP_root,"");
701    
702     // Check if the admin app is intalled on this computer and if so, add reg values for TFTP 32
703     if (RegValueExists(strATSregRoot, strRegAdminVersion))
704     { // The admin app is installed, define keys for TFTP32. These should not be installed for the control panel.
705     // Create registry values for TFTPD32
706     CreateRegistryValue(strTFTPD32RegRoot, "BaseDirectory", "");
707     CreateRegistryValue(strTFTPD32RegRoot, "Beep", 0);
708     CreateRegistryValue(strTFTPD32RegRoot, "DirText", 0);
709     CreateRegistryValue(strTFTPD32RegRoot, "Hide", 0);
710     CreateRegistryValue(strTFTPD32RegRoot, "LastWindowPos", "60 49 860 642 ");
711     CreateRegistryValue(strTFTPD32RegRoot, "LocalIP", "");
712     CreateRegistryValue(strTFTPD32RegRoot, "MaxRetransmit", 6);
713     CreateRegistryValue(strTFTPD32RegRoot, "Negociate", 1);
714     CreateRegistryValue(strTFTPD32RegRoot, "PXECompatibility", 0);
715     CreateRegistryValue(strTFTPD32RegRoot, "SaveSyslogFile", "");
716     CreateRegistryValue(strTFTPD32RegRoot, "SecurityLevel", 1);
717     CreateRegistryValue(strTFTPD32RegRoot, "Services", 2);
718     CreateRegistryValue(strTFTPD32RegRoot, "ShowProgressBar", 0);
719     CreateRegistryValue(strTFTPD32RegRoot, "TftpLogFile", "");
720     CreateRegistryValue(strTFTPD32RegRoot, "TftpPort", 69);
721     CreateRegistryValue(strTFTPD32RegRoot, "Timeout", 3);
722     CreateRegistryValue(strTFTPD32RegRoot, "UnixStrings", 1);
723     CreateRegistryValue(strTFTPD32RegRoot, "VirtualRoot", 1);
724     CreateRegistryValue(strTFTPD32RegRoot, "WinSize", 0);
725    
726     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber1", 0);
727     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionNumber2", 0);
728     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue1", "");
729     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "AddOptionValue2", "");
730     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "BootFile", "client.zpxe");
731     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DNS", 16834314);
732     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "DomainName", "");
733     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Gateway", 16834314);
734     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "IP_Pool", 335601418);
735     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "Mask", 65535);
736     CreateRegistryValue(strTFTPD32RegRoot + @"\DHCP", "PoolSize", 200);
737 william 93 }
738 william 4 }
739 william 93 #endregion
740 william 4
741 william 93 #region public static void CreateRegistryValue(string regKey, string regValue, int value)
742 william 4 // If a registry key does not exist, create it with the given default value
743 william 93 public static void CreateRegistryValue(string regKey, string regValue, int value)
744 william 4 {
745 william 93 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}",regKey, regValue, value)))
746 william 4 {
747 william 93 Logging.ATSAdminLog.InfoFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
748     Microsoft.Win32.RegistryKey objRegkey;
749    
750     try
751 william 46 {
752 william 93 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
753     }
754     catch (ArgumentNullException e)
755     {
756     MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)");
757     using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
758 william 46 {
759 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
760 william 46 {
761 william 93 using (log4net.NDC.Push("Installation info missing"))
762     {
763     Logging.ATSAdminLog.Error("Failed to create value in registry");
764     }
765 william 46 }
766     }
767 william 93 Application.Exit();
768     return;
769 william 46 }
770 william 93 catch (SecurityException e)
771 william 46 {
772 william 93 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
773     using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
774 william 46 {
775 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
776 william 46 {
777 william 93 using (log4net.NDC.Push("Invalid user rights"))
778     {
779     Logging.ATSAdminLog.Error("Failed to create value in registry");
780     }
781 william 46 }
782     }
783 william 93 Application.Exit();
784     return;
785 william 46 }
786 william 93 catch (Exception e)
787 william 46 {
788 william 93 MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
789     using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
790 william 46 {
791 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
792     {
793     Logging.ATSAdminLog.Error("Failed to create value in registry");
794    
795     }
796 william 46 }
797 william 93 Application.Exit();
798     return;
799 william 46 }
800 william 93
801     if (objRegkey != null)
802     {
803     if (objRegkey.GetValue(regValue) == null)
804     { // No reg value, create one
805     SetRegValue(regKey, regValue, value);
806     }
807 william 4 }
808 william 93 else
809     {
810     MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
811    
812 william 46 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
813     {
814 william 93 Logging.ATSAdminLog.Error("Failed to create value in registry");
815 william 46 }
816 william 93
817     Application.Exit();
818     }
819     Logging.ATSAdminLog.InfoFormat(@"Created value in Registry: {0}\{1}={2}", regKey, regValue, value);
820 william 4 }
821     }
822 william 93 #endregion
823 william 4
824 william 93 #region public static void CreateRegistryValue(string regKey, string regValue, string value)
825 william 4 // If a registry key does not exist, create it with the given default value
826     public static void CreateRegistryValue(string regKey, string regValue, string value)
827     {
828 william 93 using (log4net.NDC.Push(string.Format("key={0} name={1} data={2}", regKey, regValue, value)))
829     {
830     Logging.ATSAdminLog.InfoFormat(@"Creating value in Registry: {0}\{1}={2}", regKey, regValue, value);
831     Microsoft.Win32.RegistryKey objRegkey;
832 william 4
833 william 93 try
834 william 46 {
835 william 93 objRegkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
836     }
837     catch (ArgumentNullException e)
838     {
839     MessageBox.Show("Error when reading from registry. Installation info missing, please run the " + ATSGlobals.ApplicationName + " installation program. (92411)");
840     using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
841 william 46 {
842 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
843 william 46 {
844 william 93 using (log4net.NDC.Push("Installation info missing"))
845     {
846     Logging.ATSAdminLog.Error("Failed to create value in registry");
847     }
848 william 46 }
849     }
850 william 93 Application.Exit();
851     return;
852 william 46 }
853 william 93 catch (SecurityException e)
854 william 46 {
855 william 93 MessageBox.Show("Error when reading from registry. You do not have the necessary permission (92412). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
856     using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
857 william 46 {
858 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
859 william 46 {
860 william 93 using (log4net.NDC.Push("Invalid users rights"))
861     {
862     Logging.ATSAdminLog.Error("Failed to create value in registry");
863     }
864 william 46 }
865     }
866 william 93 Application.Exit();
867     return;
868 william 46 }
869 william 93 catch (Exception e)
870 william 46 {
871 william 93 MessageBox.Show("Error when reading from registry (92413). Key: " + regKey + " Value: " + regValue + " Error details:" + e.Message);
872     using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
873 william 46 {
874 william 93 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
875     {
876     Logging.ATSAdminLog.Error("Failed to create value in registry");
877     }
878 william 46 }
879 william 93 Application.Exit();
880     return;
881 william 46 }
882 william 4
883 william 93 if (objRegkey != null)
884     {
885     if (objRegkey.GetValue(regValue) == null)
886     { // No reg value, create one
887     SetRegValue(regKey, regValue, value);
888     }
889 william 4 }
890 william 93 else
891     {
892     MessageBox.Show("Error when reading from registry (95221). Key missing. Key: " + regKey + " Value: " + strRegTFTP_root);
893    
894 william 46 using (log4net.NDC.Push(string.Format("key={0} value={1}", regKey, regValue)))
895     {
896 william 93 Logging.ATSAdminLog.Error("Failed to create value in registry");
897 william 46 }
898 william 93
899     Application.Exit();
900     }
901     Logging.ATSAdminLog.InfoFormat(@"Created value in Registry: {0}\{1}={2}", regKey, regValue, value);
902 william 4 }
903     }
904 william 93 #endregion
905 william 4
906 william 93 #region public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
907 william 4 // Extract two integers from a string, separated by a character
908     // Result: True = extraction ok, false = invalid string.
909     // twointegers: String with two integers, separated by a character
910     // separator: the character separating the strings
911     // value1: (out) The first integer in the string
912     // value2: (out) The second integer in the string
913 william 93 public static bool ExtractIntegers(string integers, char separator, out int[] parsed_integers)
914 william 4 {
915     string[] parts;
916 william 93 parsed_integers = new int[0];
917     parts = integers.Split(separator);
918    
919     List<int> t_ints = new List<int>();
920    
921     foreach (string t in parts)
922 william 4 {
923 william 93 t_ints.Add(Convert.ToInt32(t));
924 william 4 }
925 william 93 parsed_integers = t_ints.ToArray();
926 william 4 return true;
927     }
928 william 93 #endregion
929 william 4 } // Class ATSGlobals
930     } // Namespace Anywhere TS

  ViewVC Help
Powered by ViewVC 1.1.22