Copy file and folder : File Command « File Stream « C# / C Sharp






Copy file and folder

     
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;

namespace Deployer.Logic
{
    class FileSystemUtil
    {
        /// <summary>
        /// Wraps File class enabling coping of folders
        /// </summary>
        /// <param name="source">Source path</param>
        /// <param name="dest">Destination path</param>
        public static void CopyFolder(string source, string dest)
        {
            if (!dest.EndsWith("\\"))
                dest += "\\";

            if (!Directory.Exists(dest))
                Directory.CreateDirectory(dest);

            foreach (string file in Directory.GetFiles(source))
            {
                string fileDestionation = dest + Path.GetFileName(file);
                CopyFile(file, fileDestionation);
            }

            foreach (string folder in Directory.GetDirectories(source))
            {
                string subFolder = Path.GetFileName(folder);
                CopyFolder(folder, dest + "\\" + subFolder);
            }
        }

        /// <summary>
        /// Copy folder with option to exclude some folders/files
        /// </summary>
        /// <param name="source">Source path</param>
        /// <param name="dest">Destination path</param>
        /// <param name="excludePaths">List of strings of folders/files to exclude. If it is folder it must not end with \\ in order to have successful comparison</param>
        public static void CopyFolderWithExclude(string source, string dest, List<string> excludePaths)
        {
            if (excludePaths.Contains(source.TrimEnd('\\')))
                return;

            if (!dest.EndsWith("\\"))
                dest += "\\";

            if (!Directory.Exists(dest))
                Directory.CreateDirectory(dest);

            foreach (string file in Directory.GetFiles(source))
            {
                string fileDestionation = dest + Path.GetFileName(file);

                if (!excludePaths.Contains(file))
                    CopyFile(file, fileDestionation);
            }

            foreach (string folder in Directory.GetDirectories(source))
            {
                string subFolder = Path.GetFileName(folder);
                CopyFolderWithExclude(folder, dest + "\\" + subFolder, excludePaths);
            }
        }

        /// <summary>
        /// Wraps File.Copy method because of read only and system files
        /// </summary>
        /// <param name="source">Path to file</param>
        /// <param name="destionation">Path to file</param>
        public static void CopyFile(string source, string destionation)
        {
            if (File.Exists(destionation))
                File.Delete(destionation);

            File.Copy(source, destionation);
        }

        /// <summary>
        /// Delete file from disk even if it is temporary taken by some other program
        /// </summary>
        /// <param name="filePath">Path to file</param>
        public static void DeleteFile(string filePath)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(deleteFileProcess), filePath);
        }
        private static void deleteFileProcess(object o)
        {
            try
            {
                File.Delete(o.ToString());
            }
            catch
            {
                Thread.Sleep(5000);
                deleteFileProcess(o);
                return;
            }
        }

        /// <summary>
        /// Reading file from disk with silent failing if it is taken
        /// </summary>
        /// <param name="filePath">Path to file</param>
        /// <returns>FileInfo if file is successfuly read, null if it is not</returns>
        public static FileInfo TryToReadFile(string filePath)
        {
            try
            {
                File.ReadAllBytes(filePath);
                return new FileInfo(filePath);
            }
            catch
            {
                return null;
            }
        }


        /// <summary>
        /// Deletes all content from folder
        /// </summary>
        /// <param name="dir">Path to folder</param>
        public static void ClearFolder(DirectoryInfo dir)
        {
            foreach (DirectoryInfo di in dir.GetDirectories())
                di.Delete();

            foreach (FileInfo fi in dir.GetFiles())
                fi.Delete();
        }
    }
}

   
    
    
    
    
  








Related examples in the same category

1.C# Implementation of the File Copy Command
2.Use the FileInfo Class to Delete Files with Ease
3.File Move Implementation
4.Rename File Name
5.Rename File Extension
6.Backup File
7.Copy directory
8.Find string in a file
9.Converts a number of bytes to a shorter, more readable string representation
10.Compares the specified file1. This method accepts two strings the represent two files to compare.
11.File Renamer
12.Format File Size
13.Is File Existed
14.Is File readable or writeable
15.Replace in file
16.Send a file to the recycle bin
17.Gets last write time for a file.
18.Execute File
19.Gets the file size text.
20.Get File Bytes And Readable Size Information
21.Copy folder
22.Empty a Folder