This blog is moved to
http://amalhashim.wordpress.com

Saturday, May 16, 2009

Move Folders Accross Drives or Volumes using C#

public void MoveDirectory(string strSourceDir,string strDestDir,bool bDelSource)
{
if (Directory.Exists(strSourceDir))
{
if (Directory.GetDirectoryRoot(strSou­ rceDir) == Directory.GetDirectoryRoot(strDest­ Dir))
{
Directory.Move(strSourceDir, strDestDir);
}
else
{
try
{
CopyDirectory(new DirectoryInfo(strSourceDir),new DirectoryInfo(strDestDir));
if (bDelSource)Directory.Delete(strSo­ urceDir, true);
}
catch (Exception subEx)
{
throw subEx;
}
}
}
}

private void CopyDirectory(DirectoryInfo diSourceDir,DirectoryInfo diDestDir)
{
if (!diDestDir.Exists)diDestDir.Create();­
FileInfo[] fiSrcFiles = diSourceDir.GetFiles();
foreach (FileInfo fiSrcFile in fiSrcFiles)
{
fiSrcFile.CopyTo(Path.Combine(diDe­ stDir.FullName, fiSrcFile.Name));
}
DirectoryInfo[] diSrcDirectories = diSourceDir.GetDirectories();
foreach (DirectoryInfo diSrcDirectory in diSrcDirectories)
{
CopyDirectory(diSrcDirectory, new DirectoryInfo(Path.Combine(diDestD­ ir.FullName, diSrcDirectory.Name)));
}
}

No comments: