Thursday, 12 December 2019

C#.NET - Working With Files, File and Directory

The following class has the code snippets to perform the below operations.
  • Move all files from one directory to another directory
  • Moves source file to target directory
  • Copies all files from one directory to another directory
  • Copies source file to target directory
  • Creates a Directory if it is not existed
  • Deletes the file from path

using System.IO;
using System.Linq;

public class FileProcessor
{
    /// <summary>
    /// Move all files from one directory to another directory
    /// </summary>
    /// <param name="sourceDir">Source Directory</param>
    /// <param name="targetDir">Target Directory</param>
    /// <param name="overWrite">true| overwrite the file. false|</param>
    public static void MoveFiles(string sourceDir, string targetDir, bool overWrite = false)
    {
        if (!Directory.Exists(sourceDir))
        {
            return;
        }

        CheckDirectory(targetDir);

        string[] filesToMove = Directory.GetFiles(sourceDir);

        if (filesToMove != null && filesToMove.Any())
        {
            string destinationFilePath;
            filesToMove.ToList().ForEach(
                filePath =>
                {
                    destinationFilePath = Path.Combine(targetDir, Path.GetFileName(filePath));
                    if (File.Exists(destinationFilePath))
                    {
                        if (overWrite)
                        {
                            DeleteFile(destinationFilePath);
                            File.Move(filePath, destinationFilePath);
                        }
                    }
                    else
                    {
                        File.Move(filePath, destinationFilePath);
                    }
                });
        }
    }

    /// <summary>
    /// Moves source file to target directory
    /// </summary>
    /// <param name="filepath"></param>
    /// <param name="targetDirectory"></param>
    /// <param name="overWrite"></param>
    public static void MoveFile(string sourceFilePath, string targetDirectory, bool overWrite = false)
    {
        if (!File.Exists(sourceFilePath))
        {
            return;
        }

        CheckDirectory(targetDirectory);

        string destinationFilePath = Path.Combine(targetDirectory, Path.GetFileName(sourceFilePath));

        if (overWrite)
        {
            DeleteFile(destinationFilePath);
        }

        File.Move(sourceFilePath, destinationFilePath);
    }

    /// <summary>
    /// Copies all files from one directory to another directory
    /// </summary>
    /// <param name="sourceDir"></param>
    /// <param name="destDir"></param>
    /// <param name="overWrite"></param>
    public static void CopyFiles(string sourceDir, string destDir, bool overWrite = false)
    {
        if (!Directory.Exists(sourceDir))
        {
            return;
        }

        CheckDirectory(destDir);

        string[] filesToCopy = Directory.GetFiles(sourceDir);

        if (filesToCopy != null && filesToCopy.Any())
        {
            filesToCopy.ToList().ForEach(
                sourceFilePath =>
                {
                    File.Copy(sourceFilePath, Path.Combine(destDir, Path.GetFileName(sourceFilePath)), overWrite);
                });
        }
    }

    /// <summary>
    /// Copies source file to target directory
    /// </summary>
    /// <param name="sourceFilePath"></param>
    /// <param name="directoryToCopy"></param>
    /// <param name="overWrite"></param>
    /// <returns></returns>
    public static void CopyFile(string sourceFilePath, string directoryToCopy, bool overWrite = false)
    {
        if (!File.Exists(sourceFilePath))
        {
            return;
        }

        CheckDirectory(directoryToCopy);

        string destinationFilePath = Path.Combine(directoryToCopy, Path.GetFileName(sourceFilePath));
        File.Copy(sourceFilePath, destinationFilePath, overWrite);
    }

    /// <summary>
    /// Creates a Directory if it is not existed
    /// </summary>
    /// <param name="directoryToCopy"></param>
    public static void CheckDirectory(string directoryToCopy)
    {
        if (!Directory.Exists(directoryToCopy))
        {
            Directory.CreateDirectory(directoryToCopy);
        }
    }

    /// <summary>
    /// Deletes the file from path
    /// </summary>
    /// <param name="filepath"></param>
    public static void DeleteFile(string filepath)
    {
        if (File.Exists(filepath))
        {
            File.Delete(filepath);
        }
    }
}

No comments: