gpt4 book ai didi

c# - 复制文件并备份现有的 + 子目录

转载 作者:行者123 更新时间:2023-11-30 23:09:15 28 4
gpt4 key购买 nike

我正在尝试制作一个程序来将特定文件夹中的文件以及主文件夹的子文件夹中的文件备份到另一个备份文件夹。

这是我试图实现目标的代码的一部分,但是我只备份了主文件夹中的文件,而子文件夹正在被完全复制(其中的所有文件)。

  public static string[] Backup(string sourceDirectory, string targetDirectory, string backupDirectory)
{
DirectoryInfo diBackup = new DirectoryInfo(backupDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

List<string> dups = new List<string>();
string[] fileNamesSource = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories);
string[] fileNamesDest = Directory.GetFiles(targetDirectory, "*", SearchOption.AllDirectories);
List<string> dupNS = new List<string>();
List<string> dupND = new List<string>();
List<string> BCKP = new List<string>();
string replacement = "";

for (int i = 0; i < fileNamesDest.Length; i++)
{
string res = fileNamesDest[i].Replace(targetDirectory, replacement);
dupND.Add(res);

}


foreach (var ns in fileNamesSource)
{
string res = ns.Replace(sourceDirectory, replacement);
dupNS.Add(res);

}

var duplicates = dupND.Intersect(dupNS);

string[] DuplicatesStringArray = duplicates.ToArray();

foreach (var dup in DuplicatesStringArray)
{
string res = targetDirectory + dup;
BCKP.Add(res);

}

string[] ToBeBackedUp = BCKP.ToArray();


Directory.CreateDirectory(diBackup.FullName);

// Copy each file into the new directory.
foreach (FileInfo fi in diTarget.GetFiles())
{
if (ToBeBackedUp.Contains(fi.FullName)){

fi.CopyTo(Path.Combine(diBackup.FullName, fi.Name), true);
}

}

// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in diTarget.GetDirectories())
{
if (ToBeBackedUp.Contains(diSourceSubDir.FullName)) {
DirectoryInfo nextTargetSubDir =
diBackup.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
return ToBeBackedUp;

}

关于如何只复制“源”文件夹中存在的子文件夹中的文件的任何想法?

还有 CopyAll 函数:

public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{

Directory.CreateDirectory(target.FullName);

// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{


fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);


}

// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);

}
}

提前致谢。

最佳答案

你可以这样试试容易多了

//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));

//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);

关于c# - 复制文件并备份现有的 + 子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45934382/

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