Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

A recursive method for copying files to different volumes

3.60/5 (6 votes)
14 Nov 2006 1  
This article describes a recursive method for copying files to different volumes.

Introduction

Since I found no method in .NET v1.1, to copy files to another volume, I created this recursive method to do it by myself.

Using the code

Here is the code for the FileUtilities class which defines the CopiarMoverDirectorio, CopiarDirectorio, and MoverDirectorio methods. The CopiarMoverDirectorio method contains the main logic, the other two are wrappers.

C#
using System;
using System.IO;

namespace RMORTEGA77.Utils
{
   public class FileUtilities
   {
 
      #region Copy, Move folders - Rodolfo Ortega
      /// <summary>
      /// Recursive procedure to copy or move folder to another volume
      /// </summary>
      /// <param name="d">DirectoryInfo structure of origin</param>
      /// <param name="Destino">Destiny path, including folder</param>
      /// <param name="BorraOrigen">If true, deletes origin</param>
      /// <param name="CreaDirVacios">If true move also if empty</param>
      private static void CopiarMoverDirectorio(DirectoryInfo d, 
              string Destino, bool BorraOrigen, bool CreaDirVacios) 
      { 
         // Get files.
         FileInfo[] fis = d.GetFiles();
         // Create the directory destiny, if CreaDirVacios or contains files
         if ((fis.Length > 0) || (CreaDirVacios))
            Directory.CreateDirectory(Destino);
         foreach (FileInfo fi in fis) 
         { 
            fi.CopyTo(Destino + "\\" + fi.Name);
            // Delete the origin file if BorraOrigen
            if (BorraOrigen)
               fi.Delete(); 
         }
         // Recursive copy children dirs
         DirectoryInfo[] dis = d.GetDirectories();
         foreach (DirectoryInfo di in dis) 
         {
            CopiarMoverDirectorio(di,Destino + "\\" + di.Name, 
                                  BorraOrigen, CreaDirVacios); 
         }
         // Delete the origin dir if BorraOrigen
         if (BorraOrigen)
            d.Delete();
      }

      /// <summary>
      /// Copy a folder. Wrapper for recursive procedure call.
      /// </summary>
      /// <param name="Origen">Origin path</param>
      /// <param name="Destino">Destiny path, including folder</param>
      public static void CopiarDirectorio( string Origen, string Destino)
      {
         CopiarMoverDirectorio(new DirectoryInfo(Origen), 
                               Destino, false, false);
      }

      /// <summary>
      ///  Move a folder. Wrapper for recursive procedure call.
      /// </summary>
      /// <param name="Origen">Origin path</param>
      /// <param name="Destino">Destiny path, including folder</param> 
      /// <remarks>If volume if the same,
      /// we use standard Directory.Move method </remarks>
      public static void MoverDirectorio( string Origen, string Destino)
      {
         try
         {
            Directory.Move( Origen, Destino);
         }
         catch (IOException)
         {
            CopiarMoverDirectorio(new DirectoryInfo(Origen), 
                                  Destino, true, false);
         }
      }
 
      #endregion - Rodolfo
   }

}

Points of Interest

That’s it! It works for me.

History

  • Nov. 13, 2006 - First version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here