5,557,174 members and growing! (16,954 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Files     Intermediate

A recursive method for copying files to different volumes

By rmortega77

This article describes a recursive method for copying files to different volumes.
C#, Windows, .NET 1.1, .NETVisual Studio, VS.NET2003, Dev

Posted: 14 Nov 2006
Updated: 14 Nov 2006
Views: 13,343
Bookmarked: 7 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.80 Rating: 3.60 out of 5
1 vote, 16.7%
1
0 votes, 0.0%
2
1 vote, 16.7%
3
2 votes, 33.3%
4
2 votes, 33.3%
5

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

About the Author

rmortega77


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; any contribution you could be willing to make are very wellcome... He currently needs info about new Windows Vista Shell APIs.

Submit questions related with current article to the article forum.
Occupation: Software Developer
Company: CIMEX S.A.
Location: Cuba Cuba

Other popular Files and Folders articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralSimple but helpfulmemberbookwormXP20:34 18 Jan '07  
GeneralThanks!memberfuzzymonk8:25 27 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Nov 2006
Editor: Smitha Vijayan
Copyright 2006 by rmortega77
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project