C# 如何在c中将所有文件从一个文件夹复制到另一个文件夹#
请告诉我如何将一个文件夹中的所有文件复制到c#net中的另一个文件夹 目前我正在使用:C# 如何在c中将所有文件从一个文件夹复制到另一个文件夹#,c#,.net,copy,C#,.net,Copy,请告诉我如何将一个文件夹中的所有文件复制到c#net中的另一个文件夹 目前我正在使用: int j = 1; int k = 1; for (j = 1; j < 5; j++) { for (k = 1; k < 32; k++) { string sourcePath = @Desktop_location + "\Test" + k + ".log"; if (System.
int j = 1;
int k = 1;
for (j = 1; j < 5; j++)
{
for (k = 1; k < 32; k++)
{
string sourcePath = @Desktop_location + "\Test" + k + ".log";
if (System.IO.File.Exists(sourcePath))
{
File.Copy(@Desktop_location + "\\Statistics\\Server" + j + "\Test" + k + ".log", @Desktop_location + "\\Statistics\\Transfer\\test" + j + k + ".log");
//Console.WriteLine("Test Result");
}
else
{
//Console.WriteLine("Test");
intj=1;
int k=1;
对于(j=1;j<5;j++)
{
对于(k=1;k<32;k++)
{
字符串sourcePath=@Desktop\u location+“\Test”+k+“.log”;
if(System.IO.File.Exists(sourcePath))
{
文件.Copy(@Desktop\u location+“\\Statistics\\Server”+j+“\Test”+k+“.log”、@Desktop\u location+“\\Statistics\\Transfer\\Test”+j+k+“.log”);
//控制台。写入线(“测试结果”);
}
其他的
{
//控制台写入线(“测试”);
如果您确实想复制所有文件,可以这样做(复制所有内容,包括目录):
看
请看,这里实际上并不需要它,但它包含了许多用于处理文件和文件夹的有用功能。
阅读它将有助于您规划应用程序。这有用吗?您想以某种方式对其进行改进吗?请有人帮助我如何将多个文件夹中的所有文件复制到单个文件夹中。此外,请建议是否有多个文件夹中存在同名文件,但其内容不同。这些神奇数字是什么“5”和“32”?有时更简单更好:为什么不生成xcopy?您需要告诉我们您希望发生什么,然后我们可以告诉您如何进行。否则没有正确的答案。合并、替换、异常或其他?请建议是否在多个文件夹中存在同名文件,但我希望将两个文件都保留在我创建的文件夹中我为什么要复制files@GulshanAnand这是初学者的逻辑,请自己努力编码。你永远不会从复制和粘贴别人的代码中学到什么。这取决于你希望这些重复的代码如何命名。你同意像Explorer使用的命名约定吗,即它将添加“(1)”后缀是文件名的结尾?是的,如果它附加了1、2、3等等,那么这对我来说是有效的。使用此代码只复制了一个文件。请帮助:@gulshanana,你没有做任何努力,只是让我帮你做,所以我不再愿意帮助你。这对初学者来说应该很容易解决。如果你不知道的话如何调试阅读文章。
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, destinationPath));
}
foreach (var newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(sourcePath, destinationPath));
}
string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
string myPath = @"C:\Test";
foreach (string file in filePaths)
{
FileInfo info = new FileInfo(file);
if (!File.Exists(info.FullName))
{
File.Copy(info.FullName, newPath);
}
}