You must Sign In to post a response.
  • Category: General

    In console Application i got one doubt

    how to copy one folder content to another folder?and after copying source file should be deleted,
    please provide solution with sample code
  • #753520
    Hi,

    If you want to copy directory files from one path to another then refer below sample code.


    protected void btnCopyDir_Click(object sender, EventArgs e)
    {
    try
    {
    System.IO.DirectoryInfo SrcDir = new System.IO.DirectoryInfo(txtsrc.Text);
    System.IO.DirectoryInfo DestDir = new System.IO.DirectoryInfo(txtdest.Text);
    CopyDir(SrcDir, DestDir);
    lblResult.Text = "Directories and Files Copied successfully...!!!";
    }
    catch (Exception ex)
    {
    lblResult.Text = ex.Message;
    }
    }
    protected void CopyDir(System.IO.DirectoryInfo src, System.IO.DirectoryInfo dest)
    {
    if (!dest.Exists)
    {
    dest.Create();
    System.IO.FileInfo[] files = src.GetFiles();
    foreach (System.IO.FileInfo file in files)
    {
    file.CopyTo(System.IO.Path.Combine(dest.FullName, file.Name));
    }
    System.IO.DirectoryInfo[] dirs = src.GetDirectories();
    foreach (System.IO.DirectoryInfo dir in dirs)
    {
    string destDir = System.IO.Path.Combine(dest.FullName, dir.Name);
    CopyDir(dir, new System.IO.DirectoryInfo(destDir));
    }
    }
    else if (dest.Exists)
    {

    System.IO.FileInfo[] files = src.GetFiles();
    foreach (System.IO.FileInfo file in files)
    {
    string filenamechk=System.IO.Path.Combine(dest.FullName, file.Name);
    var exists=System.IO.Directory.GetFiles(dest.FullName).Any(x=> x.Equals(filenamechk,StringComparison.OrdinalIgnoreCase));
    if(!exists)
    {
    file.CopyTo(System.IO.Path.Combine(dest.FullName, file.Name));
    }
    }
    }
    }


    for more information refer below link.

    Copy files from one directory to another.

    http://www.dotnetspider.com/resources/45480-Copying-a-Directory-and-Files.aspx

    Delete files from directory..
    http://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory

    Hope this will helpful to you..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #753524
    Please Provide the code using Console Application

  • #753542
    Hai Nagendra Prasad,
    you can use the below code snippet for the console app to perform the same operations:

    using System;
    using System.IO;
    using System.Linq;

    namespace DemoConsoleApp
    {
    class Program
    {
    static void Main(string[] args)
    {
    var sourceDirectory = new DirectoryInfo(@"D:\Test1");
    var destinationDirectory = new DirectoryInfo(@"D:\Test2");
    CopyDirectory(sourceDirectory, destinationDirectory);
    }
    private static void CopyDirectory(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory)
    {
    if (!destinationDirectory.Exists) destinationDirectory.Create();
    var sourceFiles = sourceDirectory.GetFiles();
    foreach (var file in sourceFiles)
    {
    file.CopyTo(Path.Combine(destinationDirectory.FullName, file.Name));
    }
    var sourceDirectories = sourceDirectory.GetDirectories();
    foreach (var sourceDir in sourceDirectories)
    {
    var destDir = Path.Combine(destinationDirectory.FullName, sourceDir.Name);
    CopyDirectory(sourceDir, new DirectoryInfo(destDir));
    }
    var files = sourceDirectory.GetFiles();
    foreach (var f in from file in files let checkFileName = Path.Combine(destinationDirectory.FullName, file.Name) where !Directory.GetFiles(destinationDirectory.FullName).Any(x => x.Equals(checkFileName, StringComparison.OrdinalIgnoreCase)) select file)
    {
    f.CopyTo(Path.Combine(destinationDirectory.FullName, f.Name));
    }
    sourceDirectory.Delete(true);
    }
    }
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #753549
    If in targeted folder contains same file names as in source folder,that case source folder will be deleted ,so if any same files are there than source folder should not be deleted....Please Respond

  • #753552
    Thank You So Much Sir


  • Sign In to post your comments