Nếu bạn đang tìm code c# thực hiện copy thư mục thì đây, ngay bên dưới đây là code bạn cần nhé!
private void DirectoryCopy(string sourceDir, string destDir)
{
DirectoryInfo dir = new DirectoryInfo(sourceDir);
DirectoryInfo[] dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Không tìm thấy thư mục: " + sourceDir);
}
if (!Directory.Exists(destDir))
{
Directory.CreateDirectory(destDir);
}
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDir, file.Name);
file.CopyTo(temppath,true);
}
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDir, subdir.Name);
DirectoryCopy(subdir.FullName, temppath);
}
}
Trong đó:
- sourceDir: là thư mục nguồn chứa dữ liệu cần copy.
- destDir: là thư mục chứa nội dung copy từ thư mục sourceDir.
Hy vọng hữu ích với bạn!
Nosomovo