如何将目录复制到C#中的其他驱动器?
答案 0 :(得分:16)
您可以使用此代码执行操作:
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);
}
}
低于一个也很好:
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 );
}
}
您也可以使用此功能:
FileSystem.CopyDirectory(sourceDir, destDir);
答案 1 :(得分:5)
FileSystem.CopyDirectory(sourceDir, destDir);
FileSystem.CopyDirectory
在VB名称空间和程序集中,但这可能无关紧要。
答案 2 :(得分:2)
如何:复制,删除和移动文件和文件夹(C#编程指南)
http://msdn.microsoft.com/en-us/library/cc148994.aspx
C#递归复制文件夹
http://www.csharp411.com/c-copy-folder-recursively/
答案 3 :(得分:2)
这是一个适用于.NET 4.0 +
的扩展var source = new DirectoryInfo(@"C:\Test");
var destination = new DirectoryInfo(@"E:\Test");
source.CopyTo(destination);
在项目中包含此文件
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));
}
}
}
答案 4 :(得分:0)
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;
}
答案 5 :(得分:0)
这是一种以递归方式将目录复制为async
函数的方法:
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);
}