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

Contents of /trunk/TSAdminTool/ProSupport.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 46 - (show annotations) (download)
Thu Jul 12 14:17:14 2012 UTC (9 years ago) by william
File size: 29858 byte(s)
+ add logging across ATSAdmin Tool project

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

  ViewVC Help
Powered by ViewVC 1.1.22