using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace AIFileCopyTool
{
internal class DirectoryHelper
{
///
/// Move directory between different path
///
/// The source path
/// The destination path
///
public static bool MoveDirectory(string sourcePath, string destPath)
{
try
{
var succes = CopyDirectory(sourcePath, destPath);
if (succes)
{
DeleteDirectory(sourcePath);
}
else
{
return false;
}
}
catch (Exception e)
{
return false;
}
return true;
}
///
/// Move directory between different path
///
/// The source path
/// The destination path
///
public static bool MoveDirectoryContent(string sourcePath, string destPath)
{
try
{
var succes = CopyDirectoryContent(sourcePath, destPath);
if (succes)
{
DeleteDirectory(sourcePath);
}
else
{
return false;
}
}
catch (Exception e)
{
return false;
}
return true;
}
///
/// Copy directory and files
///
/// The source path
/// The destination path
public static bool CopyDirectory(string sourcePath, string destPath)
{
try
{
DirectoryInfo sourceInfo = new DirectoryInfo(sourcePath);
var destFolder = Path.Combine(destPath, sourceInfo.Name);
CreateDirectory(destFolder);
foreach (FileInfo file in sourceInfo.GetFiles())
{
file.CopyTo(Path.Combine(destFolder, file.Name), true);
}
foreach (DirectoryInfo dirinfo in sourceInfo.GetDirectories())
{
CopyDirectory(dirinfo.FullName, destFolder);
}
}
catch (Exception ex)
{
return false;
}
return true;
}
///
/// Copy directory content to another directory.
///
/// The source path.
/// The destination path.
///
public static bool CopyDirectoryContent(string sourcePath, string destPath)
{
try
{
if (Directory.Exists(sourcePath))
{
CreateDirectory(destPath);
var sourceInfo = new DirectoryInfo(sourcePath);
foreach (FileInfo file in sourceInfo.GetFiles())
{
file.CopyTo(Path.Combine(destPath, file.Name), true);
}
foreach (DirectoryInfo dirinfo in sourceInfo.GetDirectories())
{
var destFolder = Path.Combine(destPath, dirinfo.Name);
CopyDirectoryContent(dirinfo.FullName, destFolder);
}
}
}
catch (Exception ex)
{
return false;
}
return true;
}
///
/// Copy directory content to another directory.
///
/// The source path.
/// The destination path.
///
public static bool CopyDirectoryContentWithoutDirectory(string sourcePath, string destPath)
{
try
{
if (Directory.Exists(sourcePath))
{
CreateDirectory(destPath);
var sourceInfo = new DirectoryInfo(sourcePath);
foreach (FileInfo file in sourceInfo.GetFiles())
{
file.CopyTo(Path.Combine(destPath, file.Name), true);
}
}
}
catch (Exception ex)
{
return false;
}
return true;
}
///
/// Copy directory and specific files
///
/// The source path
/// The dest path
/// The file extensions
public static bool CopyDirectoryContent(string sourcePath, string destPath, IEnumerable fileExtensions)
{
try
{
if (Directory.Exists(sourcePath))
{
CreateDirectory(destPath);
var sourceInfo = new DirectoryInfo(sourcePath);
foreach (FileInfo file in sourceInfo.GetFiles().Where(f => fileExtensions.Contains(f.Extension)))
{
file.CopyTo(Path.Combine(destPath, file.Name), true);
}
foreach (DirectoryInfo dirinfo in sourceInfo.GetDirectories())
{
var destFolder = Path.Combine(destPath, dirinfo.Name);
CopyDirectoryContent(dirinfo.FullName, destFolder, fileExtensions);
}
}
}
catch (Exception ex)
{
return false;
}
return true;
}
public static void CreateDirectory(string directory)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}
///
/// Delete directory
///
///
public static bool DeleteDirectory(string directory)
{
try
{
if (!Directory.Exists(directory))
{
return true;
}
var files = Directory.GetFiles(directory);
var directories = Directory.GetDirectories(directory);
foreach (var file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (var dir in directories)
{
DeleteDirectory(dir);
}
Directory.Delete(directory, false);
}
catch (Exception ex)
{
return false;
}
return true;
}
}
}