Click here to Skip to main content
12,406,986 members (62,903 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked

A recursive method for copying files to different volumes

, 14 Nov 2006
Rate this:
Please Sign up or sign in to vote.
This article describes a recursive method for copying files to different volumes.


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.

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))
         foreach (FileInfo fi in fis) 
            fi.CopyTo(Destino + "\\" + fi.Name);
            // Delete the origin file if BorraOrigen
            if (BorraOrigen)
         // 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)

      /// <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)
            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.


  • Nov. 13, 2006 - First version.


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


About the Author

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[] 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.

You may also be interested in...

Comments and Discussions

GeneralSimple but helpful Pin
bookwormXP18-Jan-07 19:34
memberbookwormXP18-Jan-07 19:34 
GeneralThanks! Pin
fuzzymonk27-Nov-06 7:25
memberfuzzymonk27-Nov-06 7:25 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160730.1 | Last Updated 14 Nov 2006
Article Copyright 2006 by rmortega77
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid