Copy folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Crude.Core
{
public static class FileSystemUtility
{
public static void CopyFile(string sourceFileName, string destFileName)
{
FileInfo fileInfo = new FileInfo(destFileName);
if (!fileInfo.Directory.Exists)
{
fileInfo.Directory.Create();
}
File.Copy(new FileInfo(sourceFileName).FullName, destFileName);
}
public static void CopyFolder(string sourceFolder, string destFolder)
{
if (!Directory.Exists(destFolder))
{
Directory.CreateDirectory(destFolder);
}
string[] files = Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
string name = Path.GetFileName(file);
string dest = Path.Combine(destFolder, name);
File.Copy(file, dest, true);
}
string[] folders = Directory.GetDirectories(sourceFolder);
foreach (string folder in folders)
{
string name = Path.GetFileName(folder);
string dest = Path.Combine(destFolder, name);
CopyFolder(folder, dest);
}
}
}
}
Related examples in the same category