How to copy the Images from one folder to another folder using C#.Net

 

using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Copying_Images_to_Desktop_Directory
{
    public partial class Form1 : Form
    {
        String DestinationPath;
        public Form1()
        {
            InitializeComponent();
        }
        public static void CopyDirectory(string sourceDirectory, string targetDirectory)
        {
            DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
            DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
            CopyAll(diSource, diTarget);
        }
        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            // Check if the target directory exists, if not, create it.
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }

            // Copy each file into it's new directory.
            foreach (FileInfo fi in source.GetFiles())
            {
                fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
            }
            // Copy each subdirectory using recursion.
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);

            }
        }

//This code is used for selecting the images form Debug foler and that is copied to Desktop folder.
//If desktop folder is not there, it automatically created in desktop
        private void BtCopy_Click(object sender, EventArgs e)
        {
            String ImagePath = Path.Combine(Application.StartupPath, "SourceFile\\Data");
            if (!Directory.Exists(ImagePath))
                Directory.CreateDirectory(ImagePath);
            string strPath = Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
            DestinationPath = Path.Combine(strPath, "ImageFiles");
            if (Directory.Exists(DestinationPath))
            {
                Directory.Delete(DestinationPath, true);
            }
            Directory.CreateDirectory(DestinationPath);
            CopyDirectory(ImagePath, DestinationPath);
        }
    }
}

Ebook Download
View all
Learn
View all