C# 将文件夹中的所有内容复制到两个文件目标文件夹中
我想将文件夹中的所有内容复制到两个文件目标文件夹中C# 将文件夹中的所有内容复制到两个文件目标文件夹中,c#,C#,我想将文件夹中的所有内容复制到两个文件目标文件夹中 foreach (string newPath in Directory.GetFiles(@"E:\autotransfer", "*.*", SearchOption.AllDirectories)) File.Copy(newPath, newPath.Replace(@"E:\autotransfer", @"E:\autotransferbackup"), true);
foreach (string newPath in Directory.GetFiles(@"E:\autotransfer", "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(@"E:\autotransfer",
@"E:\autotransferbackup"), true);
foreach (string newPath in Directory.GetFiles(@"E:\autotransfer", "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(@"E:\autotransfer",
@"E:\autotransferbackupcp"), true);
您可以使用此代码,有关更多信息,请参阅此处的答案: 如果目录结构不同,则需要检查文件夹是否存在,如果不存在,请先创建文件夹,然后复制文件。摘自msdn上的此处: 您可以复制此函数并在代码中使用它 希望这有帮助
using System;
using System.IO;
class DirectoryCopyExample
{
static void Main()
{
// Copy from the current directory, include subdirectories.
DirectoryCopy(".", @".\temp", true);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
请说得更具体些。除了使用
string.Replace()
来操作文件路径不是一个好主意之外,您发布的一小部分代码似乎可以工作。或者至少做点什么。你说它“不起作用”。具体来说,代码以什么方式不起作用?提供一个可靠地再现您的问题的好方法,并准确地解释代码的作用,以及您希望它做什么。(请注意,GetFiles()
返回的路径的大小写可能与您的Replace()
调用中的大小写不匹配。)谢谢您,先生。它是有效的。我的下一个问题是,它如何在每次文件夹自动传输有内容时自动传输,并自动发送到两个目录。因为我是编程新手,我需要一些帮助。谢谢你,先生。如果我只想复制某个目录中两个子文件夹的内容,该怎么办?谢谢你的回复。我首先尝试了上面的第一个建议/答案,它对我有效。也许在其他情况下,我会试试你的代码,先生。谢谢你,先生。
using System;
using System.IO;
class DirectoryCopyExample
{
static void Main()
{
// Copy from the current directory, include subdirectories.
DirectoryCopy(".", @".\temp", true);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}