Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

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.

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

Share

About the Author

rmortega77
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 PinmemberbookwormXP18-Jan-07 19:34 
GeneralThanks! Pinmemberfuzzymonk27-Nov-06 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140926.1 | Last Updated 14 Nov 2006
Article Copyright 2006 by rmortega77
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid