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

Annotation of /trunk/TSAdminTool/Globals.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 46 - (hide annotations) (download)
Thu Jul 12 14:17:14 2012 UTC (7 years, 11 months ago) by william
File size: 44844 byte(s)
+ add logging across ATSAdmin Tool project

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

  ViewVC Help
Powered by ViewVC 1.1.22