Làm cách nào để sao chép thư mục sang ổ đĩa khác trong C#?Sao chép thư mục sang một ổ đĩa khác
Trả lời
Bạn có thể sử dụng mã này để thực hiện hoạt động của bạn:
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);
}
}
dưới một cũng tốt:
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);
}
}
bạn có thể sử dụng chức năng này cũng:
FileSystem.CopyDirectory(sourceDir, destDir);
FileSystem.CopyDirectory(sourceDir, destDir);
FileSystem.CopyDirectory
nằm trong không gian tên và lắp ráp VB, nhưng điều đó có thể không quan trọng.
Làm thế nào để: sao chép, xóa, và Move Files and Folders (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/cc148994.aspx
C# Sao chép thư mục đệ quy
http://www.csharp411.com/c-copy-folder-recursively/
Điều đó không cho thấy cách thực hiện một bản sao đệ quy. –
@Matthew: Đã sửa lỗi. –
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;
}
Sẽ dễ dàng hơn cho những người khác đọc câu trả lời của bạn nếu bạn định dạng mã đúng cách. Ngoài ra, tốt nhất là thêm một số ngữ cảnh vào mã của bạn. Viết về lý do tại sao bạn cho rằng đây là câu trả lời hay hay câu trả lời khác so với các câu trả lời khác. – mdewitt
Dưới đây là một phần mở rộng sẽ làm việc trong .NET 4.0+
var source = new DirectoryInfo(@"C:\Test");
var destination = new DirectoryInfo(@"E:\Test");
source.CopyTo(destination);
Bao gồm tệp này trong dự án của bạn
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));
}
}
}
Dưới đây là một phương pháp sao chép một thư mục đệ quy như một chức năng 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);
}
- 1. VBA để sao chép tệp từ một thư mục này sang một thư mục khác
- 2. Sao chép nội dung của một thư mục này sang một thư mục khác
- 3. Cách sao chép tệp từ thư mục sang thư mục khác
- 4. Sao chép tệp từ thư mục này sang thư mục khác bằng cách sử dụng vbscripting
- 5. Sao chép tệp từ thư mục này sang thư mục khác bằng Ruby
- 6. Làm cách nào để sao chép các tệp của tôi từ một thư mục này sang một thư mục khác?
- 7. Làm cách nào để di chuyển thư mục 'Đoạn mã của tôi' của Visual Studio sang một ổ đĩa khác?
- 8. Hadoop sao chép một thư mục?
- 9. Tôi có thể sao chép .git sang một thư mục khác không?
- 10. Cách sao chép tệp từ một thư mục sang thư mục khác bằng cách tạo thư mục nếu thư mục đó không tồn tại
- 11. Trong FTP, làm cách nào để sao chép tệp từ xa sang các thư mục khác
- 12. Sao chép thư mục trong C#
- 13. Làm cách nào để sao chép 10 tệp gần đây nhất từ thư mục này sang thư mục khác?
- 14. Tại sao sau khi sao chép kho git sang một thư mục khác, tôi đã sửa đổi các tệp?
- 15. Shell Command để sao chép một thư mục đến một vị trí khác
- 16. Sao chép tập tin trên một ổ đĩa mạng chia sẻ
- 17. Sao chép một mảng chuỗi sang một chuỗi khác
- 18. Sao chép một cấu trúc sang một cấu trúc khác
- 19. Cách tốt nhất để sao chép thư mục và tất cả thư mục con và tệp bằng C#
- 20. sao chép biểu đồ (adjacency_list) sang một biểu đồ khác
- 21. Sao chép nguyên tử một bảng MySQL sang bảng khác?
- 22. Sao chép cấu trúc thư mục (tệp sans) từ một vị trí khác
- 23. Cách Sao chép Tệp Hình ảnh từ Bộ sưu tập sang một thư mục khác theo lập trình trong Android
- 24. Robocopy - Sao chép nội dung thư mục vào một thư mục đơn
- 25. Không sao chép vào thư mục bin
- 26. Sao chép tập tin với cấu trúc chính xác vào một thư mục sử dụng Xcopy
- 27. Nội dung tối ưu 2: Sao chép tệp lưu/sao lưu vào thư mục khác
- 28. Sao chép thư mục bằng NSIS.
- 29. IOKhông đủ dung lượng ổ đĩa khi truy cập ổ đĩa gắn Citrix
- 30. Sao chép tất cả các tập tin trong thư mục
lol, những gì? Xin lỗi, đừng làm bất cứ ai nghĩ rằng câu hỏi này là vui nhộn theo cách nó được hỏi :) – VoodooChild
Tôi hiểu rằng câu hỏi đã được trả lời, nhưng bạn nên thử và cải thiện chất lượng của câu hỏi. Nó không được viết rất tốt. –
Tôi nghĩ chúng ta cần một lý do mới để đóng - 'dễ tìm nếu OP làm nỗ lực' – Gishu