123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace AIFileCopyTool
- {
- internal class DirectoryHelper
- {
- /// <summary>
- /// Move directory between different path
- /// </summary>
- /// <param name="sourcePath">The source path</param>
- /// <param name="destPath">The destination path</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Move directory between different path
- /// </summary>
- /// <param name="sourcePath">The source path</param>
- /// <param name="destPath">The destination path</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Copy directory and files
- /// </summary>
- /// <param name="sourcePath">The source path</param>
- /// <param name="destPath">The destination path</param>
- 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;
- }
- /// <summary>
- /// Copy directory content to another directory.
- /// </summary>
- /// <param name="sourcePath">The source path.</param>
- /// <param name="destPath">The destination path.</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Copy directory content to another directory.
- /// </summary>
- /// <param name="sourcePath">The source path.</param>
- /// <param name="destPath">The destination path.</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Copy directory and specific files
- /// </summary>
- /// <param name="sourcePath">The source path</param>
- /// <param name="destPath">The dest path</param>
- /// <param name="extensions">The file extensions</param>
- public static bool CopyDirectoryContent(string sourcePath, string destPath, IEnumerable<string> 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);
- }
- }
- /// <summary>
- /// Delete directory
- /// </summary>
- /// <param name="directory"></param>
- 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;
- }
- }
- }
|