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
private static void CopiarMoverDirectorio(DirectoryInfo d,
string Destino, bool BorraOrigen, bool CreaDirVacios)
{
FileInfo[] fis = d.GetFiles();
if ((fis.Length > 0) || (CreaDirVacios))
Directory.CreateDirectory(Destino);
foreach (FileInfo fi in fis)
{
fi.CopyTo(Destino + "\\" + fi.Name);
if (BorraOrigen)
fi.Delete();
}
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)
{
CopiarMoverDirectorio(di,Destino + "\\" + di.Name,
BorraOrigen, CreaDirVacios);
}
if (BorraOrigen)
d.Delete();
}
public static void CopiarDirectorio( string Origen, string Destino)
{
CopiarMoverDirectorio(new DirectoryInfo(Origen),
Destino, false, false);
}
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.