How do I copy a directory to a different drive开发者_JAVA百科 in C#?
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);
FileSystem.CopyDirectory(sourceDir, destDir);
FileSystem.CopyDirectory
is in a VB namespace and assembly, but that probably doesn't matter.
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/
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));
}
}
}
private String path;
public int copyAllContents(String destinationFolder, ProgressBar progressBar)
{
int countCopyFiles = 0;
if (!Directory.Exists(destinationFolder))
{ Directory.CreateDirectory(destinationFolder); }
String[] allFilesForCurrentFolder = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly);
String[] subFoldersAllpath = Directory.GetDirectories(path);
for (int i = 0; i < allFilesForCurrentFolder.Length; i++)
{
try { File.Copy(allFilesForCurrentFolder[i], destinationFolder + "\\" + Path.GetFileName(allFilesForCurrentFolder[i])); countCopyFiles++; progressBar.Value++; }
catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); }
}
if (subFoldersAllpath.Length == 0)
{ return allFilesForCurrentFolder.Length; };
for (int i = 0; i < subFoldersAllpath.Length; i++)
{
this.path = subFoldersAllpath[i];
String[] subFoldersAllpathLastFolder = subFoldersAllpath[i].Split('\\');
countCopyFiles += this.copyAllContents(destinationFolder + "\\" + subFoldersAllpathLastFolder[subFoldersAllpathLastFolder.Length - 1], progressBar);
}
return countCopyFiles;
}
Here's an approach that copies a directory recursively as an async
function:
public static async Task CopyDirectoryAsync(string sourceDirectory, string destinationDirectory)
{
if (!Directory.Exists(destinationDirectory))
Directory.CreateDirectory(destinationDirectory);
foreach (var file in Directory.GetFiles(sourceDirectory))
{
var name = Path.GetFileName(file);
var dest = Path.Combine(destinationDirectory, name);
await CopyFileAsync(file, dest);
}
foreach (var subdir in Directory.GetDirectories(sourceDirectory))
{
var name = Path.GetFileName(subdir);
var dest = Path.Combine(destinationDirectory, name);
await CopyDirectoryAsync(subdir, dest);
}
}
public static async Task CopyFileAsync(string sourceFile, string destinationFile)
{
using (var sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan))
using (var destinationStream = new FileStream(destinationFile, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan))
await sourceStream.CopyToAsync(destinationStream);
}
精彩评论