TechAid24

Your Technical Consultant

Building a High-Performance File Copier in C# Using FileStream

Posted on by Selim Ahmed 1 minute read

In this post, we want to discuss How to create a file copier with C#. Before we get started, if you want to know about the Encryption and Decryption method using C#, please go through the following article: Working with Encryption and Decryption using C#.
 
DirectoryInfo sourcedinfo = new DirectoryInfo(@”D:\FromImage”);
DirectoryInfo Destination = new DirectoryInfo(@”D:\ToImage”);

public void CopyFile(DirectoryInfo source, DirectoryInfo target)
{
   try
     {
      //check if the target directory exists
      if (Directory.Exists(target.FullName) == false)
      {
            Directory.CreateDirectory(target.FullName);
      }

      //copy all the files into the new directory
      foreach (FileInfo fi in source.GetFiles())
      {
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
      }

      //copy all the sub directories using recursion
      foreach (DirectoryInfo diSourceDir in source.GetDirectories())
      {
            DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
            CopyFile(diSourceDir, nextTargetDir);
      }

      MessageBox.Show(“Copy Successfully”, “Confirmation”);
   }
   catch (IOException ie)
   {
      MessageBox.Show(ie.Message);
   }
}

Thank you for reading Create File Copier with C#.

Create File Copier with C#

Tags:

Selim Ahmed

Thank you for visiting my personal blog. I’m Selim Ahmed, a tech-savvy who loves to learn a new technology every day. Innovation, research, and development is my daily activities. I love to work in a place where I can learn, innovate, and develop new things.Please feel free to reach me on any comments and feedback you have. I would be more than glad to listen and reply.

Leave a Reply

Your email address will not be published. Required fields are marked *