Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#
Article

A recursive method for copying files to different volumes

Rate me:
Please Sign up or sign in to vote.
3.60/5 (6 votes)
14 Nov 2006 35.6K   12   2
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


Written By
Software Developer CIMEX S.A.
Cuba Cuba
Rodolfo Ortega is a Cuban Computer Scientist. He works as IT Auditor for the CIMEX S.A. subsidiary in Holguin, Cuba. He lives and works in Holguin, in the eastern part of the island of Cuba.

You can contact him at rodolfom[]cimex.com.cu for any personal message: Ideas on new articles, bibliography about new APIs, questions, are wellcome.

Submit questions related with current article to the article forum.

Comments and Discussions

 
GeneralSimple but helpful Pin
bookwormXP18-Jan-07 19:34
bookwormXP18-Jan-07 19:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.