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

Annotation of /trunk/TSAdminTool/Globals.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 99 - (hide annotations) (download)
Sat Jul 14 04:50:27 2012 UTC (9 years, 2 months ago) by william
File size: 46968 byte(s)
+ bulk commit

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

  ViewVC Help
Powered by ViewVC 1.1.22