gpt4 book ai didi

.net - 如何复制一个文件夹的所有内容(复制文件和文件夹)

转载 作者:行者123 更新时间:2023-12-04 00:24:15 26 4
gpt4 key购买 nike

我想复制所有文件,复制所有文件夹,包括空文件夹和 protected 文件,不可见的文件,文件夹或文件!全部复制!

最佳答案

以下示例取自 MSDN's documentation of the DirectoryInfo class显示如何复制目录:

using System;
using System.IO;

class CopyDir
{
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
if (source.FullName.ToLower() == target.FullName.ToLower())
{
return;
}

// 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);
}
}

public static void Main()
{
string sourceDirectory = @"c:\sourceDirectory";
string targetDirectory = @"c:\targetDirectory";

DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

CopyAll(diSource, diTarget);
}

// Output will vary based on the contents of the source directory.
}

关于.net - 如何复制一个文件夹的所有内容(复制文件和文件夹),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6009724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com