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

Annotation of /trunk/TSAdminTool/Globals.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 149 - (hide annotations) (download)
Sun Jul 15 11:21:36 2012 UTC (9 years, 4 months ago) by william
File size: 49047 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22