Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a directory to a different drive

Tags:

c#

How do I copy a directory to a different drive in C#?

like image 857
Govind Malviya Avatar asked Jun 01 '10 04:06

Govind Malviya


People also ask

Can you copy a directory?

In Windows 10 and earlier versions, right-click the folder and select Copy, or click Edit and then Copy. Navigate to the location where you want to place the folder and all its contents. icon on the menu bar. Alternatively, right-click the folder, select Show more options and then Paste.

How do I copy a directory to another location in Terminal?

Similarly, you can copy an entire directory to another directory using cp -r followed by the directory name that you want to copy and the name of the directory to where you want to copy the directory (e.g. cp -r directory-name-1 directory-name-2 ).

How do you copy the contents of a directory to another?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option.


4 Answers

You can use this code to perform your operation:

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())
    {
        Console.WriteLine(@”Copying {0}\{1}”, target.FullName, fi.Name);
        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);
    }
}

below one is also good:

    static public void CopyFolder( string sourceFolder, string destFolder )
    {
        if (!Directory.Exists( destFolder ))
            Directory.CreateDirectory( destFolder );
        string[] files = Directory.GetFiles( sourceFolder );
        foreach (string file in files)
        {
            string name = Path.GetFileName( file );
            string dest = Path.Combine( destFolder, name );
            File.Copy( file, dest );
        }
        string[] folders = Directory.GetDirectories( sourceFolder );
        foreach (string folder in folders)
        {
           string name = Path.GetFileName( folder );
           string dest = Path.Combine( destFolder, name );
            CopyFolder( folder, dest );
        }
    }

you can use this function also:

FileSystem.CopyDirectory(sourceDir, destDir);
like image 94
Dr. Rajesh Rolen Avatar answered Oct 17 '22 10:10

Dr. Rajesh Rolen


FileSystem.CopyDirectory(sourceDir, destDir);

FileSystem.CopyDirectory is in a VB namespace and assembly, but that probably doesn't matter.

like image 40
Matthew Flaschen Avatar answered Oct 17 '22 12:10

Matthew Flaschen


How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/cc148994.aspx

C# Copy Folder Recursively
http://www.csharp411.com/c-copy-folder-recursively/

like image 20
Robert Harvey Avatar answered Oct 17 '22 11:10

Robert Harvey


Here's an extension that will work in .NET 4.0+

var source = new DirectoryInfo(@"C:\Test");
var destination = new DirectoryInfo(@"E:\Test");
source.CopyTo(destination);

Include this file in your project

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

namespace System.IO
{
  public static class DirectoryInfoExtensions
  {
    public static void CopyTo(this DirectoryInfo source, DirectoryInfo target)
    {
      if (!target.Exists)
        target.Create();

      foreach (var file in source.GetFiles())
        file.CopyTo(Path.Combine(target.FullName, file.Name), true);

      foreach (var subdir in source.GetDirectories())
        subdir.CopyTo(target.CreateSubdirectory(subdir.Name));
    }
  }
}
like image 38
Robear Avatar answered Oct 17 '22 11:10

Robear