PageRenderTime 41ms CodeModel.GetById 23ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/IZWebFileManager/Components/xDirectory.cs

http://izwebfilemanager.googlecode.com/
C# | 258 lines | 97 code | 31 blank | 130 comment | 22 complexity | 229e339a62d13f7f0d93c747dc0003e7 MD5 | raw file
  1using System;
  2using System.IO;
  3using System.Collections.Generic;
  4using System.Text;
  5
  6//#////////////////////////////////////////#//
  7//# Iterative Version of xDirectory.Copy() #//
  8//# Coder: John D. Storer II               #//
  9//# Date:  Thursday, May 18th, 2006        #//
 10//# Tool:  Visual C# 2005 Express Edition  #//
 11//#////////////////////////////////////////#//
 12
 13namespace System.IO
 14{
 15	/// <summary>
 16	/// xDirectory v2.0 - Copy a Source Directory and it's SubDirectories/Files.
 17	/// Coder: John Storer II
 18	/// Date: Thursday, May 18, 2006
 19	/// </summary>
 20	internal class xDirectory
 21	{
 22		/// <summary>
 23		/// Default Overwrite Value - Change to Preference.
 24		/// </summary>
 25		private static bool _DefaultOverwrite = false;
 26
 27		/// <summary>
 28		/// Default Folder Iteration Limit - Change to Preference.
 29		/// </summary>
 30		private static int _DefaultIterationLimit = 1000000;
 31
 32		///////////////////////////////////////////////////////////
 33		/////////////////// String Copy Methods ///////////////////
 34		///////////////////////////////////////////////////////////
 35
 36		/// <summary>
 37		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
 38		/// </summary>
 39		/// <param name="sSource">The Source Directory</param>
 40		/// <param name="sDestination">The Destination Directory</param>
 41		public static void Copy (string sSource, string sDestination) {
 42			Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), null, null, _DefaultOverwrite, _DefaultIterationLimit);
 43		}
 44
 45		/// <summary>
 46		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
 47		/// </summary>
 48		/// <param name="sSource">The Source Directory</param>
 49		/// <param name="sDestination">The Destination Directory</param>
 50		/// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
 51		public static void Copy (string sSource, string sDestination, bool Overwrite) {
 52			Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), null, null, Overwrite, _DefaultIterationLimit);
 53		}
 54
 55		/// <summary>
 56		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
 57		/// </summary>
 58		/// <param name="sSource">The Source Directory</param>
 59		/// <param name="sDestination">The Destination Directory</param>
 60		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
 61		public static void Copy (string sSource, string sDestination, string FileFilter) {
 62			Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, null, _DefaultOverwrite, _DefaultIterationLimit);
 63		}
 64
 65		/// <summary>
 66		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
 67		/// </summary>
 68		/// <param name="sSource">The Source Directory</param>
 69		/// <param name="sDestination">The Destination Directory</param>
 70		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
 71		/// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
 72		public static void Copy (string sSource, string sDestination, string FileFilter, bool Overwrite) {
 73			Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, null, Overwrite, _DefaultIterationLimit);
 74		}
 75
 76		/// <summary>
 77		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
 78		/// </summary>
 79		/// <param name="sSource">The Source Directory</param>
 80		/// <param name="sDestination">The Destination Directory</param>
 81		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
 82		/// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
 83		public static void Copy (string sSource, string sDestination, string FileFilter, string DirectoryFilter) {
 84			Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, DirectoryFilter, _DefaultOverwrite, _DefaultIterationLimit);
 85		}
 86
 87		/// <summary>
 88		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
 89		/// </summary>
 90		/// <param name="sSource">The Source Directory</param>
 91		/// <param name="sDestination">The Destination Directory</param>
 92		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
 93		/// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
 94		/// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
 95		public static void Copy (string sSource, string sDestination, string FileFilter, string DirectoryFilter, bool Overwrite) {
 96			Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, DirectoryFilter, Overwrite, _DefaultIterationLimit);
 97		}
 98
 99		/// <summary>
100		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
101		/// </summary>
102		/// <param name="sSource">The Source Directory</param>
103		/// <param name="sDestination">The Destination Directory</param>
104		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
105		/// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
106		/// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
107		/// <param name="FolderLimit">Iteration Limit - Total Number of Folders/SubFolders to Copy</param>
108		public static void Copy (string sSource, string sDestination, string FileFilter, string DirectoryFilter, bool Overwrite, int FolderLimit) {
109			Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, DirectoryFilter, Overwrite, FolderLimit);
110		}
111
112		//////////////////////////////////////////////////////////////////
113		/////////////////// DirectoryInfo Copy Methods ///////////////////
114		//////////////////////////////////////////////////////////////////
115
116		/// <summary>
117		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
118		/// </summary>
119		/// <param name="diSource">The Source Directory</param>
120		/// <param name="diDestination">The Destination Directory</param>
121		public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination) {
122			Copy (diSource, diDestination, null, null, _DefaultOverwrite, _DefaultIterationLimit);
123		}
124
125		/// <summary>
126		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
127		/// </summary>
128		/// <param name="diSource">The Source Directory</param>
129		/// <param name="diDestination">The Destination Directory</param>
130		/// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
131		public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, bool Overwrite) {
132			Copy (diSource, diDestination, null, null, Overwrite, _DefaultIterationLimit);
133		}
134
135		/// <summary>
136		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
137		/// </summary>
138		/// <param name="diSource">The Source Directory</param>
139		/// <param name="diDestination">The Destination Directory</param>
140		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
141		public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter) {
142			Copy (diSource, diDestination, FileFilter, null, _DefaultOverwrite, _DefaultIterationLimit);
143		}
144
145		/// <summary>
146		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
147		/// </summary>
148		/// <param name="diSource">The Source Directory</param>
149		/// <param name="diDestination">The Destination Directory</param>
150		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
151		/// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
152		public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter, bool Overwrite) {
153			Copy (diSource, diDestination, FileFilter, null, Overwrite, _DefaultIterationLimit);
154		}
155
156		/// <summary>
157		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
158		/// </summary>
159		/// <param name="diSource">The Source Directory</param>
160		/// <param name="diDestination">The Destination Directory</param>
161		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
162		/// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
163		public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter, string DirectoryFilter) {
164			Copy (diSource, diDestination, FileFilter, DirectoryFilter, _DefaultOverwrite, _DefaultIterationLimit);
165		}
166
167		/// <summary>
168		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
169		/// </summary>
170		/// <param name="diSource">The Source Directory</param>
171		/// <param name="diDestination">The Destination Directory</param>
172		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
173		/// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
174		/// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
175		public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter, string DirectoryFilter, bool Overwrite) {
176			Copy (diSource, diDestination, FileFilter, DirectoryFilter, Overwrite, _DefaultIterationLimit);
177		}
178
179
180		/////////////////////////////////////////////////////////////////////
181		/////////////////// The xDirectory.Copy() Method! ///////////////////
182		/////////////////////////////////////////////////////////////////////
183
184
185		/// <summary>
186		/// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
187		/// </summary>
188		/// <param name="diSource">The Source Directory</param>
189		/// <param name="diDestination">The Destination Directory</param>
190		/// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
191		/// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
192		/// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
193		/// <param name="FolderLimit">Iteration Limit - Total Number of Folders/SubFolders to Copy</param>
194		public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter, string DirectoryFilter, bool Overwrite, int FolderLimit) {
195			int iterator = 0;
196			List<DirectoryInfo> diSourceList = new List<DirectoryInfo> ();
197			List<FileInfo> fiSourceList = new List<FileInfo> ();
198
199			try {
200				///// Error Checking /////
201				if (diSource == null)
202					throw new ArgumentException ("Source Directory: NULL");
203				if (diDestination == null)
204					throw new ArgumentException ("Destination Directory: NULL");
205				if (!diSource.Exists)
206					throw new IOException ("Source Directory: Does Not Exist");
207				if (!(FolderLimit > 0))
208					throw new ArgumentException ("Folder Limit: Less Than 1");
209				if (DirectoryFilter == null || DirectoryFilter == string.Empty)
210					DirectoryFilter = "*";
211				if (FileFilter == null || FileFilter == string.Empty)
212					FileFilter = "*";
213
214				///// Add Source Directory to List /////
215				diSourceList.Add (diSource);
216
217				///// First Section: Get Folder/File Listing /////
218				while (iterator < diSourceList.Count && iterator < FolderLimit) {
219					foreach (DirectoryInfo di in diSourceList [iterator].GetDirectories (DirectoryFilter))
220						diSourceList.Add (di);
221
222					foreach (FileInfo fi in diSourceList [iterator].GetFiles (FileFilter))
223						fiSourceList.Add (fi);
224
225					iterator++;
226				}
227
228				///// Second Section: Create Folders from Listing /////
229				foreach (DirectoryInfo di in diSourceList) {
230					if (di.Exists) {
231						string sFolderPath = diDestination.FullName + di.FullName.Remove (0, diSource.FullName.Length);
232
233						///// Prevent Silly IOException /////
234						if (!Directory.Exists (sFolderPath))
235							Directory.CreateDirectory (sFolderPath);
236					}
237				}
238
239				///// Third Section: Copy Files from Listing /////
240				foreach (FileInfo fi in fiSourceList) {
241					if (fi.Exists) {
242						string sFilePath = diDestination.FullName + fi.FullName.Remove (0, diSource.FullName.Length);
243
244						///// Better Overwrite Test W/O IOException from CopyTo() /////
245						if (Overwrite)
246							fi.CopyTo (sFilePath, true);
247						else {
248							///// Prevent Silly IOException /////
249							if (!File.Exists (sFilePath))
250								fi.CopyTo (sFilePath, true);
251						}
252					}
253				}
254			}
255			catch { throw; }
256		}
257	}
258}