I was in need of a CopyFolder function for the FolderManager --- FolderManager.Instance.CopyFolder().
Not having found one, I wrote one. If this is useful, please feel free to use it. If you have any suggestions or modifications, let me know.
//Usage
var sourceFolder = FolderManager.Instance.GetFolder(PortalId, "Assets/_default/");
var rootDestFolder = FolderManager.Instance.GetFolder(PortalId, "Assets/");
//Copy a folder to a different root folder with a new folder name
FolderManager.Instance.CopyFolder(sourceFolder, rootDestFolder,"myNewFolderName");
//copy a folder to the same root folder with a new foldername
FolderManager.Instance.CopyFolder(sourceFolder, sourceFolder, "myNewFolderName");
//copy a folder to a new destination within the same portal
FolderManager.Instance.CopyFolder(PortalId, sourceFolder, "Assets/myNewFolderName/");
//Implementation
using System;
using System.IO;
using System.Linq;
using System.Web;
using DotNetNuke.Services.FileSystem;
namespace DNNExtensions
{
public static class Extensions
{
/// <summary>
/// Copy an existing folder and contents to the a new folder in the destination. After copying, the destination will
/// contain a folder with the same name as the source folder.
/// </summary>
/// <param name="ifm"></param>
/// <param name="source">An exising folder</param>
/// <param name="rootDest">The parent folder into which the new folder will be created</param>
public static void CopyFolder(this IFolderManager ifm, IFolderInfo source, IFolderInfo rootDest)
{
if (!ifm.FolderExists(rootDest.PortalID, rootDest.FolderPath+$"{source.FolderName}/"))
{
rootDest= ifm.AddFolder(rootDest.PortalID, rootDest.FolderPath+$"{source.FolderName}/");
}
var diSource = source.PhysicalPath;
var diDest = rootDest.PhysicalPath;
_copyFolder(diSource,diDest,rootDest.PortalID);
ifm.Synchronize(rootDest.PortalID,rootDest.FolderPath);
}
/// <summary>
/// Copy an existing folder and contents to the a new folder in the destination. After copying, the destination will
/// contain a folder with the specifed newFolderName.
/// </summary>
/// <param name="ifm"></param>
/// <param name="source">An exising folder</param>
/// <param name="rootDest">The parent folder info into which the new folder will be created</param>
/// <param name="newFolderName">The name of the new folder</param>
public static IFolderInfo CopyFolder(this IFolderManager ifm, IFolderInfo source, IFolderInfo rootDest, string newFolderName)
{
var newDest=!ifm.FolderExists(rootDest.PortalID, rootDest.FolderPath+newFolderName+"/")?ifm.AddFolder(rootDest.PortalID, rootDest.FolderPath+newFolderName+"/"):ifm.GetFolder(rootDest.PortalID, rootDest.FolderPath+newFolderName+"/");
var diSource = source.PhysicalPath;
var diDest = newDest.PhysicalPath;
_copyFolder(diSource,diDest,newDest.PortalID);
ifm.Synchronize(rootDest.PortalID,rootDest.FolderPath,true,true);
return newDest;
}
/// <summary>
/// Copy an existing folder and contents to a new folder using the destination path.
/// </summary>
/// <param name="ifm"></param>
/// <param name="portalId">The portal for the new folder</param>
/// <param name="source">An exising folder</param>
/// <param name="destPath">The path and name of the new folder within the portal</param>
public static IFolderInfo CopyFolder(this IFolderManager ifm, int portalId, IFolderInfo source, string destPath)
{
var newDest=!ifm.FolderExists(portalId, destPath)?ifm.AddFolder(portalId, destPath):ifm.GetFolder(portalId,destPath);
var diSource=source.PhysicalPath;
_copyFolder(diSource,newDest.PhysicalPath,portalId);
ifm.Synchronize(portalId,newDest.FolderPath,true,true);
return newDest;
}
/// <summary>
///
/// </summary>
/// <param name="ifm"></param>
/// <param name="source"></param>
/// <param name="dest"></param>
/// <param name="recurse">Copy subfolders and files, too</param>
/// <returns></returns>
public static IFolderInfo CopyFiles(this IFileManager ifm, IFolderInfo source, IFolderInfo dest, Boolean recurse=false)
{
var diSource = new DirectoryInfo(source.PhysicalPath);
var diDest = new DirectoryInfo(dest.PhysicalPath);
_copyAll(diSource, diDest, recurse);
return dest;
}
private static void _copyFolder(string sourceDirectory, string targetDirectory, int portalId)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
_copyAll(diSource, diTarget);
}
private static void _copyAll(DirectoryInfo source, DirectoryInfo target, Boolean recurse=true)
{
var dirList = source.GetDirectories().Where(p=>p.FullName+@"\"!=target.FullName&&p.FullName!=target.FullName);
// Copy each file into the new directory.
foreach (System.IO.FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
}
// Copy each subdirectory using recursion.
if (!recurse) return;
foreach (DirectoryInfo diSourceSubDir in dirList)
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
_copyAll(diSourceSubDir, nextTargetSubDir);
}
}
}
}