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

Contents of /trunk/TSAdminTool/ProSupport.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 99 - (show annotations) (download)
Sat Jul 14 04:50:27 2012 UTC (7 years, 11 months ago) by william
File size: 31825 byte(s)
+ bulk commit

1
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Text;
6 using System.Windows.Forms;
7 using System.Globalization;
8 using System.Data;
9 using System.Security.AccessControl;
10 using System.Security.Principal;
11 using System.IO;
12 using System.Management;
13 using System.Net;
14 using log4net;
15
16 // Support class for the AnywhereTS version. This file should only be included in the Pro version.
17 namespace AnywhereTS
18 {
19 public static class ProSupport
20 {
21 // File names
22 public const string strHostsFilename = "hosts"; // The name of the thin clients host file.
23 public const string strNetworkConfigFilename = "network"; // Name of the Thinstation common config file in the TFTP root directory.
24 public const string strDatabaseFilename = "ats.mdf"; // Name of the AnywhereTS SQL express database file.
25 public const string strDatabaseFilename2 = "ats_log.ldf"; // Name of the AnywhereTS SQL express database log file.
26
27 // Registry keys
28 public const string strRegTFTP_root = "TFTP_root"; // Reg key for TFTP root directory in database
29 public const string strRegDatabaseDir = "DatabaseDir"; // Reg key for database directory
30 public const string strRegAnywhereTSServer = "ATSServer"; // Reg key for database server
31 public const string strRegDatabaseServer = "DatabaseServer"; // Reg key for database server
32 public const string strRegDatabaseInstance = "DatabaseInstance"; // Reg key for database server
33 public const string strRegDestDir = "DestDir"; // Reg key for Destination directory, used in unmanged mode..
34
35 // Database constants
36 public const string DEFAULT_RECORD_MAC = "Default "; // Phony MAC address (12 chars) used to identify the client record that contains the default settings for all new clients
37
38 // Database variables
39 public static atsDataSetTableAdapters.ClientTableAdapter clientTableAdapter; // The AnywhereTS application data adapter for the clients table. Only this adapter should be used.
40 public static atsDataSetTableAdapters.TftpServerTableAdapter tftpServerTableAdapter; // The AnywhereTS application data adapter for the TFTP servers table. Only this adapter should be used.
41 public static atsDataSetTableAdapters.TerminalServerTableAdapter terminalServerTableAdapter; // The AnywhereTS application data adapter for the terminal servers table. Only this adapter should be used.
42 public static atsDataSetTableAdapters.AppInfoTableAdapter appInfoTableAdapter; // The AnywhereTS application data adapter for the AppInfo table. Only this adapter should be used.
43
44 public static string strAnywhereTSServer; // If this computer does not contain the database server, this is the name of the database server that contains the ATS database. Used by the Control Panel
45 public static string strDatabaseServer; // If this computer does not contain the database server, this is the name of the database server that contains the ATS database. Used by the Control Panel
46 public static string strDatabaseInstance; // If this computer does not contain the database server, this is the name of the database server that contains the ATS database. Used by the Control Panel
47
48 // Directories
49 public static string strDatabasePath; // The directory where the database file is located.
50 public static string strDestDir; // Destination directory for client files in unmanaged mode.
51
52 // Constructor Initializes the global variables
53 static ProSupport()
54 {
55 Logging.Initialize();
56 // Initiate variables from registry
57 strAnywhereTSServer = ATSGlobals.GetATSRegValueString(strRegAnywhereTSServer);
58 strDatabasePath = ATSGlobals.GetATSRegValueString(strRegDatabaseDir);
59 strDestDir = ATSGlobals.GetATSRegValueString(strRegDestDir);
60 strDatabaseServer = ATSGlobals.GetATSRegValueString(strRegDatabaseServer);
61 strDatabaseInstance = ATSGlobals.GetATSRegValueString(strRegDatabaseInstance);
62 }
63
64 // Loook up local IP for computer
65 public static void ThisComputerIp (System.Windows.Forms.ComboBox combo, bool bolServerName)
66 {
67 ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
68
69 ManagementObjectCollection queryCollection = query.Get();
70 combo.Items.Clear();
71 if (bolServerName == true) //Add computer name
72 {
73 combo.Items.Add(Dns.GetHostName());
74 }
75 foreach (ManagementObject mo in queryCollection)
76 {
77 string[] addresses = (string[])mo["IPAddress"];
78 foreach (string ipaddress in addresses)
79 {
80 if (ipaddress !="0.0.0.0")
81 combo.Items.Add(ipaddress);
82
83 }
84 }
85 }
86
87 // Compate two DriveInfo arrays and return TRUE if they are equal.
88 public static bool CompareDriveInfoArrays(List<DriveInfo> data1, List<DriveInfo> data2)
89 {
90 // If both are null, they're equal
91 if (data1 == null && data2 == null)
92 {
93 return true;
94 }
95 // If either but not both are null, they're not equal
96 if (data1 == null || data2 == null)
97 {
98 return false;
99 }
100 if (data1.Count != data2.Count)
101 {
102 return false;
103 }
104 for (int i = 0; i < data1.Count; i++)
105 {
106 if (data1[i].Name + data1[i].VolumeLabel != data2[i].Name + data2[i].VolumeLabel)
107 {
108 return false;
109 }
110 }
111 return true;
112 }
113
114 // Get an IP parameter (IP, netmask, gateway) from the local machine
115 // Paramters:
116 // Out: IP address in the format "x.x.x.x"
117 // strNetworkConf = "IPAddress": Get IPAddress
118 // strNetworkConf = "IPSubnet": Get netmask
119 // strNetworkConf = "DefaultIPGateway": Get gateway
120 // strNetworkConf = "Description": Get network card
121 // strNetworkConf = "MACAddress": Get MAC address
122 // strNetworkConf = "DNSServerSearchOrder": Get DNS
123 public static string ThisComputerNetworkConf(string strNetworkConf)
124 {
125 ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
126 ManagementObjectCollection queryCollection = query.Get();
127 foreach (ManagementObject mo in queryCollection)
128 {
129 string[] addresses = (string[])mo[strNetworkConf];
130 return addresses[0];
131 }
132 return ""; // No network adapter found.
133 }
134
135 // Initiate the database
136 public static void InitDatabase()
137 {
138 //// Create and set the connection string
139 //string dataServerName; // The name of the database server in the connection string
140 //if (strDatabaseServer.Length != 0)
141 //{ // We have a database server
142 // dataServerName = strDatabaseServer;
143 //}
144 //else
145 //{ // We do not have an external, data server.
146 // dataServerName = ".";
147 //}
148 //Properties.Settings.Default["atsConnectionString"] = @"Data Source=" + dataServerName + @"\SQLEXPRESS;Database='AnywhereTS';Integrated Security=True;Connect Timeout=30;User Instance=False";
149 //Properties.Settings.Default.Save();
150
151 // Set up table adapters
152 clientTableAdapter = new atsDataSetTableAdapters.ClientTableAdapter();
153 tftpServerTableAdapter = new atsDataSetTableAdapters.TftpServerTableAdapter();
154 terminalServerTableAdapter = new atsDataSetTableAdapters.TerminalServerTableAdapter();
155 appInfoTableAdapter = new atsDataSetTableAdapters.AppInfoTableAdapter();
156 }
157
158 // Adds an ACL entry for RW access on the specified file for Remote desktop users.
159 [Obsolete("GrantRWaccessForRemoteDesktopUsers has been deprecated - define user access rights via SQL Server")]
160 public static void GrantRWaccessForRemoteDesktopUsers(string fileName)
161 {
162 // Get a FileSecurity object that represents the current security settings.
163 FileSecurity fSecurity = File.GetAccessControl(fileName);
164
165 // Add FileSystemAccessRule:s to the security settings.
166 // Remote desktop users can read and write, but not delete
167 fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinRemoteDesktopUsersSid, null), FileSystemRights.Read, AccessControlType.Allow));
168 fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinRemoteDesktopUsersSid, null), FileSystemRights.Write, AccessControlType.Allow));
169
170 // Set the new access settings.
171 File.SetAccessControl(fileName, fSecurity);
172 }
173
174 // Adds an ACL entry for ReadWriteModify access on the specified file for Remote desktop users.
175 [Obsolete("GrantRWMaccessForRemoteDesktopUsers has been deprecated - define user access rights via SQL Server")]
176 public static void GrantRWMaccessForRemoteDesktopUsers(string fileName)
177 {
178 // Get a FileSecurity object that represents the current security settings.
179 FileSecurity fSecurity = File.GetAccessControl(fileName);
180
181 // Add FileSystemAccessRule:s to the security settings.
182 // Remote desktop users can read and write, but not delete
183 fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinRemoteDesktopUsersSid, null), FileSystemRights.Read, AccessControlType.Allow));
184 fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinRemoteDesktopUsersSid, null), FileSystemRights.Write, AccessControlType.Allow));
185 fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinRemoteDesktopUsersSid, null), FileSystemRights.Modify, AccessControlType.Allow));
186
187 // Set the new access settings.
188 File.SetAccessControl(fileName, fSecurity);
189 }
190
191 // (obsolete)
192 // Adds an ACL entry for to deny modify access on the specified file for all users.
193 [Obsolete("DenyAccessRightModifyForUsers has been deprecated - define user access rights via SQL Server")]
194 public static void DenyAccessRightModifyForUsers(string fileName)
195 {
196 // Get a FileSecurity object that represents the current security settings.
197 FileSecurity fSecurity = File.GetAccessControl(fileName);
198
199 // Add FileSystemAccessRule:s to the security settings.
200 // Remote desktop users can read and write, but not delete
201 fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), FileSystemRights.Write, AccessControlType.Deny));
202 fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), FileSystemRights.Modify, AccessControlType.Deny));
203
204 // Set the new access settings.
205 File.SetAccessControl(fileName, fSecurity);
206 }
207
208
209 // Writes config files to all of the TFTP directories definied in registry and database,
210 // and set rights.
211 public static void WriteConfigFiles(ATSImageRuntimeConfig config, string fileName, bool setRights)
212 { // config: The config that should be written to disk
213 // fileName: Name of the config file to write
214 // setRights: True = Set access rights on the file, so that all remote desktop users can delete the file.
215 // Write client config file
216
217 if (ATSGlobals.tftpConfig == 0)
218 { // Use ATS TFTP
219 WriteConfigFile(config, ATSGlobals.strTFTPdir + @"\" + fileName, setRights);
220 }
221 else if (ATSGlobals.tftpConfig == 1)
222 { // External TFTP
223 // Write the config file to all TFTP directories
224 atsDataSet.TftpServerDataTable datatableTFTP; // TFTP server directories
225 atsDataSetTableAdapters.TftpServerTableAdapter tftpServerTableAdapter; // The AnywhereTS application data adapter for the TFTP servers table. Only this adapter should be used.
226
227 tftpServerTableAdapter = new AnywhereTS.atsDataSetTableAdapters.TftpServerTableAdapter();
228 datatableTFTP = new atsDataSet.TftpServerDataTable();
229
230 tftpServerTableAdapter.Fill(datatableTFTP);
231 foreach (DataRow row in datatableTFTP.Rows)
232 {
233 WriteConfigFile(config, GetTftpPath(row) + @"\" + fileName, setRights);
234 }
235 }
236 else
237 {
238 string error = "Error: Unknown TFTP Config mode (28158)";
239 using (log4net.NDC.Push(string.Format("tftpConfig={0}", ATSGlobals.tftpConfig)))
240 {
241 Logging.ATSAdminLog.Error(error);
242 }
243 MessageBox.Show(string.Format("{0} -> {1}", error, string.Format("tftpConfig={0}", ATSGlobals.tftpConfig)));
244 return;
245 }
246 }
247
248 // Copy image file to all external tftp directories/servers
249 public static void CopyImageFile(string path, string sourceFileName, string destFileName)
250 {
251 atsDataSet.TftpServerDataTable datatableTFTP; // TFTP server directories
252 datatableTFTP = new atsDataSet.TftpServerDataTable();
253 atsDataSetTableAdapters.TftpServerTableAdapter tftpServerTableAdapter; // The AnywhereTS application data adapter for the TFTP servers table. Only this adapter should be used.
254 tftpServerTableAdapter = new AnywhereTS.atsDataSetTableAdapters.TftpServerTableAdapter();
255
256 tftpServerTableAdapter.Fill(datatableTFTP);
257 foreach (DataRow row in datatableTFTP.Rows)
258 {
259 // Copy to the TFTP server
260 try
261 {
262 File.Copy(path + @"\" + sourceFileName, GetTftpPath(row) + @"\" + destFileName, true);
263 }
264 catch (Exception e)
265 {
266 MessageBox.Show("Could not update TFTP server '" + GetTftpPath(row) + "' (22181). Error: " + e.Message);
267 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
268 {
269 using (log4net.NDC.Push(string.Format("tftpPath={0}", GetTftpPath(row))))
270 {
271 Logging.ATSAdminLog.Error("Could not update TFTP server");
272 }
273 }
274 }
275
276 // Try to set rights for image the image file
277 // (not implemented)
278 /*
279 try
280 {
281 DenyAccessRightModifyForUsers(row["Path"].ToString() + @"\" + file);
282 }
283 catch (Exception e)
284 {
285 MessageBox.Show("Could not set rights for image file on TFTP server '" + row["Path"].ToString() + "' (22183). Error: " + e.Message);
286 } */
287 }
288 }
289
290 // Create subdirectory in all external tftp directories/servers
291 public static void CreateDirectoryOnTFTP_servers(string directoryName)
292 {
293 atsDataSet.TftpServerDataTable datatableTFTP; // TFTP server directories
294 datatableTFTP = new atsDataSet.TftpServerDataTable();
295 atsDataSetTableAdapters.TftpServerTableAdapter tftpServerTableAdapter; // The AnywhereTS application data adapter for the TFTP servers table. Only this adapter should be used.
296 tftpServerTableAdapter = new AnywhereTS.atsDataSetTableAdapters.TftpServerTableAdapter();
297
298 tftpServerTableAdapter.Fill(datatableTFTP);
299 foreach (DataRow row in datatableTFTP.Rows)
300 {
301 // Create the Directory on the TFTP server
302 try
303 {
304 Directory.CreateDirectory(GetTftpPath(row) + @"\" + directoryName);
305 }
306 catch (Exception e)
307 {
308 MessageBox.Show("Could not update TFTP server '" + GetTftpPath(row) + "' (22381). Error: " + e.Message);
309 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
310 {
311 using (log4net.NDC.Push(string.Format("tftpPath={0}", GetTftpPath(row))))
312 {
313 Logging.ATSAdminLog.Error("Could not update TFTP server");
314 }
315 }
316 }
317 }
318 }
319
320 public static void WriteConfigFile(ATSImageRuntimeConfig config, string path, bool setRights)
321 {
322 config.WriteConfigFile(path);
323
324 if (setRights)
325 {
326 // Set access rights for the config file.
327 try
328 {
329 // Add an access control entry to the config file.
330 ProSupport.GrantRWMaccessForRemoteDesktopUsers(path);
331 }
332 catch (Exception ex)
333 {
334 MessageBox.Show("Cannot set access rights for client configuration. Application will abort. Error: " + ex.Message);
335 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
336 {
337 Logging.ATSAdminLog.Error("Cannot set access rights for client configuration");
338 }
339 Application.Exit();
340 return;
341 }
342 }
343 }
344
345
346 // Writes host files to all of the TFTP directories definied in registry and database.
347 public static void WriteHostsFiles()
348 {
349 if (ATSGlobals.tftpConfig == 0)
350 { // Use ATS TFTP
351 WriteHostsFile(ATSGlobals.strTFTPdir + @"\" + strHostsFilename);
352 }
353 else if (ATSGlobals.tftpConfig == 1)
354 { // External TFTP
355 // Write the config file to all TFTP directories
356 atsDataSet.TftpServerDataTable datatableTFTP; // TFTP server directories
357 datatableTFTP = new atsDataSet.TftpServerDataTable();
358 tftpServerTableAdapter.Fill(datatableTFTP);
359 foreach (DataRow row in datatableTFTP.Rows)
360 {
361 WriteHostsFile(GetTftpPath(row) + @"\" + strHostsFilename);
362 }
363 }
364 else
365 {
366 string error = "Error: Unknown TFTP Config mode (48158)";
367 using (log4net.NDC.Push(string.Format("tftpConfig={0}", ATSGlobals.tftpConfig)))
368 {
369 Logging.ATSAdminLog.Error(error);
370 }
371 MessageBox.Show(string.Format("{0} -> {1}", error, string.Format("tftpConfig={0}", ATSGlobals.tftpConfig)));
372 return;
373 }
374 }
375
376
377 // Create a (new) encrypted hosts file from data in the database.
378 private static void WriteHostsFile(string path)
379 {
380 atsDataSet datasetClient = new atsDataSet();
381 ProSupport.clientTableAdapter.FillClients(datasetClient.Client);
382
383 // System.IO.StreamWriter writer = null;
384 CryptStream writer = null;
385
386 // Delete old hosts file
387 if (System.IO.File.Exists(path))
388 {
389 System.IO.File.Delete(path);
390 }
391
392 // Write a new hosts file.
393 try
394 {
395 //writer = new System.IO.StreamWriter(path);
396 writer = new CryptStream(path);
397 writer.NewLine = "\n"; // Unix style line terminators
398 writer.WriteLine("# This is a hosts file generated by AnywhereTS. Do not change.");
399 foreach (DataRow row in datasetClient.Client.Rows)
400 {
401 writer.WriteLine(row["ClientName"].ToString() + " " + row["MacAddress"].ToString());
402 }
403 writer.WriteLine("# Detta e slutet."); // Termination string to avoid decryptation bug.
404 }
405 catch (Exception e)
406 {
407 //improve autoretry
408 MessageBox.Show("Error: Could not write configuration data to disk. Possibly the data is being accessed by another component right now or you do not have the sufficient rights. Please retry this operation later (52044). Error details: " + e.Message);
409 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
410 {
411 using (log4net.NDC.Push(string.Format("path={0}", path)))
412 {
413 Logging.ATSAdminLog.Error("Could not write configuration data to disk");
414 }
415 }
416 }
417
418 finally
419 {
420 if (writer != null)
421 writer.Close();
422 }
423
424 // DenyAccessRightModifyForUsers(path); // Prevent users from deleting or modifying the hosts file.
425 }
426
427
428 // Check if user is administrator
429 // Returns true if user is administrator
430 public static bool IsAnAdministrator()
431 {
432 WindowsIdentity identity =
433 WindowsIdentity.GetCurrent();
434 WindowsPrincipal principal =
435 new WindowsPrincipal(identity);
436 return principal.IsInRole
437 (WindowsBuiltInRole.Administrator);
438 }
439
440 public static string GetMacAddressFromNameAndDatabase(string strName)
441 {
442 string macAddress;
443 // Try to extract MAC address from client name.
444 macAddress = ATSGlobals.GetMacFromATSname(strName);
445 if (macAddress.Length == 0)
446 { // MAC not in name, try to lookup client name in database
447 atsDataSet datasetClientName = new atsDataSet();
448 // Update dataset with the client row matching the name
449 try
450 {
451 ProSupport.clientTableAdapter.FillOnName(datasetClientName.Client, strName);
452 }
453 catch
454 { // Error when accessing database
455 return "";
456 }
457 if (datasetClientName.Client.Rows.Count > 0)
458 { // Client row found in database
459 macAddress = datasetClientName.Client.Rows[0]["MacAddress"].ToString();
460 }
461 else
462 { // Name not found
463 macAddress = "";
464 }
465 }
466 return macAddress;
467 }
468
469 // Re-writes all config files, so that they will be containing new parameters.
470 // Also creates config files if they have been deleted, i.e by an expired beta.
471 public static void RecreateConfigFiles()
472 {
473 // Re-create all config files
474 if (ATSGlobals.tftpConfig == 0)
475 { // Use ATS TFTP
476 WriteConfigFiles(ATSGlobals.strTFTPdir);
477 }
478 else if (ATSGlobals.tftpConfig == 1)
479 { // External TFTP
480 // Write the config file to all TFTP directories
481 atsDataSet.TftpServerDataTable datatableTFTP; // TFTP server directories
482 datatableTFTP = new atsDataSet.TftpServerDataTable();
483 tftpServerTableAdapter.Fill(datatableTFTP);
484 foreach (DataRow row in datatableTFTP.Rows)
485 {
486 // Write to TFTP path
487 WriteConfigFiles(GetTftpPath(row));
488 }
489 }
490 else
491 {
492 string error = "Error: Unknown TFTP Config mode (88158)";
493 using (log4net.NDC.Push(string.Format("tftpConfig={0}", ATSGlobals.tftpConfig)))
494 {
495 Logging.ATSAdminLog.Error(error);
496 }
497 MessageBox.Show(string.Format("{0} -> {1}", error, string.Format("tftpConfig={0}", ATSGlobals.tftpConfig)));
498 return;
499 }
500
501 WriteHostsFiles();
502 ATSImageRuntimeConfig currentConfig = new ATSImageRuntimeConfig();
503 currentConfig.ReadDefaultFromDatabase();
504 ProSupport.WriteConfigFiles(currentConfig, ProSupport.strNetworkConfigFilename, false);
505 }
506
507
508 static void WriteConfigFiles(string strDirectory)
509 {
510 ATSImageRuntimeConfig currentConfig = new ATSImageRuntimeConfig();
511 atsDataSet datasetClient = new atsDataSet();
512 ProSupport.clientTableAdapter.FillClients(datasetClient.Client);
513 string strFilePath;
514 foreach (DataRow row in datasetClient.Client.Rows)
515 {
516 // Write the config file corresponding to the client
517 currentConfig.ReadFromDatabase(row);
518 strFilePath = strDirectory + @"\" + row["MacAddress"];
519 currentConfig.WriteConfigFile(strFilePath);
520 }
521 }
522
523 // Return a local or UNC path for TFTP server, depending on what it best.
524 public static string GetTftpPath(DataRow tftpRow)
525 {
526 // Extract computer name from UNC Path
527 string uncPath = tftpRow["Path"].ToString();
528 string uncComputer = uncPath.Substring(2, uncPath.IndexOf(@"\", 2) - 2);
529 if (uncComputer.ToUpper() == Environment.MachineName)
530 { // The UNC path resides on this computer, use a local path if it is available
531 if (tftpRow["LocalPath"].ToString().Length > 0)
532 { // We have a local path
533 return tftpRow["LocalPath"].ToString();
534 }
535 else
536 { // No local path, we have to go with the UNC path referring to the local machine.
537 return tftpRow["Path"].ToString();
538 }
539 }
540 else
541 { // The UNC path does not reside on this computer, use it as it is.
542 return tftpRow["Path"].ToString();
543 }
544 }
545
546
547 // Sub routines used by wizard for screen resolution
548 //-----------------------------------------------------
549
550 public static void UpdateScreenResolutionControls(int x, int y, TextBox txtScreenResX, TextBox txtScreenResY, TrackBar tbrScreenResolution)
551 {
552 txtScreenResX.Text = x.ToString();
553 txtScreenResY.Text = y.ToString();
554
555 for (int i = 0; i < ATSGlobals.ScreenResolutions.GetLength(0); i++)
556 {
557 if (x == ATSGlobals.ScreenResolutions[i, 0] && y == ATSGlobals.ScreenResolutions[i, 1])
558 {
559 tbrScreenResolution.Value = i + 1;
560 return;
561 }
562
563 }
564 // Values could not be expressed using the track bar. Set it at default.
565 tbrScreenResolution.Value = 4;
566 }
567
568 public static void GetScreenResolutionFromTrackbar(TrackBar tbr, out int x, out int y)
569 {
570 x = ATSGlobals.ScreenResolutions[tbr.Value - 1, 0];
571 y = ATSGlobals.ScreenResolutions[tbr.Value - 1, 1];
572 }
573
574 public static void UpdateResolutionTrackbar(TextBox txtScreenResX, TextBox txtScreenResY, TrackBar tbrScreenResolution)
575 { // Try to convert x, y values to trackbar.
576 int x, y;
577 try
578 {
579 x = Int32.Parse(txtScreenResX.Text.Trim());
580 }
581 catch (Exception)
582 {
583 x = 0;
584 }
585
586 try
587 {
588 y = Int32.Parse(txtScreenResY.Text.Trim());
589 }
590 catch (Exception)
591 {
592 y = 0;
593 }
594
595 UpdateScreenResolutionControls(x, y, txtScreenResX, txtScreenResY, tbrScreenResolution);
596 }
597
598
599
600 public static void copyDirectory(string Src,string Dst)
601 {
602 String[] Files;
603
604 if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
605 Dst+=Path.DirectorySeparatorChar;
606 if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
607 Files=Directory.GetFileSystemEntries(Src);
608 foreach(string Element in Files){
609 // Sub directories
610
611 if(Directory.Exists(Element))
612 copyDirectory(Element,Dst+Path.GetFileName(Element));
613 // Files in directory
614
615 else
616 File.Copy(Element,Dst+Path.GetFileName(Element), true);
617 }
618 }
619
620
621
622
623 } // end class Prosupport
624
625
626 public class CryptStream : StreamWriter
627 {
628 BinaryWriter outStream;
629
630 const byte offset1 = 36;
631 const byte offset2 = 0xab;
632 const byte key = 0x36;
633 byte value;
634
635 public CryptStream(string s)
636 : base(s)
637 {
638 // Base constructor has created a stream. Steal it.
639 outStream = new BinaryWriter(base.BaseStream);
640 WriteHeader();
641 }
642
643
644 public CryptStream(Stream s)
645 : base(s)
646 {
647 outStream = new BinaryWriter(s);
648 WriteHeader();
649 }
650
651
652 private void WriteHeader()
653 {
654 outStream.Write((byte)0x41);
655 outStream.Write((byte)0x54);
656 outStream.Write((byte)0x53);
657 outStream.Write((byte)0x78);
658 value = key;
659 }
660
661
662 private byte Crypt(byte ch1)
663 {
664 byte ch2 = (byte)((ch1 + offset1) & 0xFF);
665 byte ch3 = (byte)((ch2 ^ value) & 0xFF);
666 value = (byte)((value + offset2) & 0xFF);
667 return ch3;
668 }
669
670 public override void Write(string s)
671 {
672 for (int i = 0; i < s.Length; i++)
673 outStream.Write(Crypt((byte)s[i]));
674 }
675
676
677 public override void WriteLine(string s)
678 {
679 Write(s);
680 Write("\n");
681 }
682 }
683
684 } //end namespace

  ViewVC Help
Powered by ViewVC 1.1.22