xDirectory.cs :  » Network-Clients » IZWebFileManager » System » IO » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » IZWebFileManager 
IZWebFileManager » System » IO » xDirectory.cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

//#////////////////////////////////////////#//
//# Iterative Version of xDirectory.Copy() #//
//# Coder: John D. Storer II               #//
//# Date:  Thursday, May 18th, 2006        #//
//# Tool:  Visual C# 2005 Express Edition  #//
//#////////////////////////////////////////#//

namespace System.IO{
  /// <summary>
  /// xDirectory v2.0 - Copy a Source Directory and it's SubDirectories/Files.
  /// Coder: John Storer II
  /// Date: Thursday, May 18, 2006
  /// </summary>
  internal class xDirectory
  {
    /// <summary>
    /// Default Overwrite Value - Change to Preference.
    /// </summary>
    private static bool _DefaultOverwrite = false;

    /// <summary>
    /// Default Folder Iteration Limit - Change to Preference.
    /// </summary>
    private static int _DefaultIterationLimit = 1000000;

    ///////////////////////////////////////////////////////////
    /////////////////// String Copy Methods ///////////////////
    ///////////////////////////////////////////////////////////

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="sSource">The Source Directory</param>
    /// <param name="sDestination">The Destination Directory</param>
    public static void Copy (string sSource, string sDestination) {
      Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), null, null, _DefaultOverwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="sSource">The Source Directory</param>
    /// <param name="sDestination">The Destination Directory</param>
    /// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
    public static void Copy (string sSource, string sDestination, bool Overwrite) {
      Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), null, null, Overwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="sSource">The Source Directory</param>
    /// <param name="sDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    public static void Copy (string sSource, string sDestination, string FileFilter) {
      Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, null, _DefaultOverwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="sSource">The Source Directory</param>
    /// <param name="sDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
    public static void Copy (string sSource, string sDestination, string FileFilter, bool Overwrite) {
      Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, null, Overwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="sSource">The Source Directory</param>
    /// <param name="sDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    public static void Copy (string sSource, string sDestination, string FileFilter, string DirectoryFilter) {
      Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, DirectoryFilter, _DefaultOverwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="sSource">The Source Directory</param>
    /// <param name="sDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
    public static void Copy (string sSource, string sDestination, string FileFilter, string DirectoryFilter, bool Overwrite) {
      Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, DirectoryFilter, Overwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="sSource">The Source Directory</param>
    /// <param name="sDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
    /// <param name="FolderLimit">Iteration Limit - Total Number of Folders/SubFolders to Copy</param>
    public static void Copy (string sSource, string sDestination, string FileFilter, string DirectoryFilter, bool Overwrite, int FolderLimit) {
      Copy (new DirectoryInfo (sSource), new DirectoryInfo (sDestination), FileFilter, DirectoryFilter, Overwrite, FolderLimit);
    }

    //////////////////////////////////////////////////////////////////
    /////////////////// DirectoryInfo Copy Methods ///////////////////
    //////////////////////////////////////////////////////////////////

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="diSource">The Source Directory</param>
    /// <param name="diDestination">The Destination Directory</param>
    public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination) {
      Copy (diSource, diDestination, null, null, _DefaultOverwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="diSource">The Source Directory</param>
    /// <param name="diDestination">The Destination Directory</param>
    /// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
    public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, bool Overwrite) {
      Copy (diSource, diDestination, null, null, Overwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="diSource">The Source Directory</param>
    /// <param name="diDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter) {
      Copy (diSource, diDestination, FileFilter, null, _DefaultOverwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="diSource">The Source Directory</param>
    /// <param name="diDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
    public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter, bool Overwrite) {
      Copy (diSource, diDestination, FileFilter, null, Overwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="diSource">The Source Directory</param>
    /// <param name="diDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter, string DirectoryFilter) {
      Copy (diSource, diDestination, FileFilter, DirectoryFilter, _DefaultOverwrite, _DefaultIterationLimit);
    }

    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="diSource">The Source Directory</param>
    /// <param name="diDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
    public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter, string DirectoryFilter, bool Overwrite) {
      Copy (diSource, diDestination, FileFilter, DirectoryFilter, Overwrite, _DefaultIterationLimit);
    }


    /////////////////////////////////////////////////////////////////////
    /////////////////// The xDirectory.Copy() Method! ///////////////////
    /////////////////////////////////////////////////////////////////////


    /// <summary>
    /// xDirectory.Copy() - Copy a Source Directory and it's SubDirectories/Files
    /// </summary>
    /// <param name="diSource">The Source Directory</param>
    /// <param name="diDestination">The Destination Directory</param>
    /// <param name="FileFilter">The File Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="DirectoryFilter">The Directory Filter (Standard Windows Filter Parameter, Wildcards: "*" and "?")</param>
    /// <param name="Overwrite">Whether or not to Overwrite a Destination File if it Exists.</param>
    /// <param name="FolderLimit">Iteration Limit - Total Number of Folders/SubFolders to Copy</param>
    public static void Copy (DirectoryInfo diSource, DirectoryInfo diDestination, string FileFilter, string DirectoryFilter, bool Overwrite, int FolderLimit) {
      int iterator = 0;
      List<DirectoryInfo> diSourceList = new List<DirectoryInfo> ();
      List<FileInfo> fiSourceList = new List<FileInfo> ();

      try {
        ///// Error Checking /////
        if (diSource == null)
          throw new ArgumentException ("Source Directory: NULL");
        if (diDestination == null)
          throw new ArgumentException ("Destination Directory: NULL");
        if (!diSource.Exists)
          throw new IOException ("Source Directory: Does Not Exist");
        if (!(FolderLimit > 0))
          throw new ArgumentException ("Folder Limit: Less Than 1");
        if (DirectoryFilter == null || DirectoryFilter == string.Empty)
          DirectoryFilter = "*";
        if (FileFilter == null || FileFilter == string.Empty)
          FileFilter = "*";

        ///// Add Source Directory to List /////
        diSourceList.Add (diSource);

        ///// First Section: Get Folder/File Listing /////
        while (iterator < diSourceList.Count && iterator < FolderLimit) {
          foreach (DirectoryInfo di in diSourceList [iterator].GetDirectories (DirectoryFilter))
            diSourceList.Add (di);

          foreach (FileInfo fi in diSourceList [iterator].GetFiles (FileFilter))
            fiSourceList.Add (fi);

          iterator++;
        }

        ///// Second Section: Create Folders from Listing /////
        foreach (DirectoryInfo di in diSourceList) {
          if (di.Exists) {
            string sFolderPath = diDestination.FullName + di.FullName.Remove (0, diSource.FullName.Length);

            ///// Prevent Silly IOException /////
            if (!Directory.Exists (sFolderPath))
              Directory.CreateDirectory (sFolderPath);
          }
        }

        ///// Third Section: Copy Files from Listing /////
        foreach (FileInfo fi in fiSourceList) {
          if (fi.Exists) {
            string sFilePath = diDestination.FullName + fi.FullName.Remove (0, diSource.FullName.Length);

            ///// Better Overwrite Test W/O IOException from CopyTo() /////
            if (Overwrite)
              fi.CopyTo (sFilePath, true);
            else {
              ///// Prevent Silly IOException /////
              if (!File.Exists (sFilePath))
                fi.CopyTo (sFilePath, true);
            }
          }
        }
      }
      catch { throw; }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.