C# 自动备份脚本中的重复输出
Repeated Outputs in C# Automatic Backup script
我从多个在线资源创建了一个自动备份脚本,并且我对 C# 的了解有限,但出于某种原因,它重复了第一行写入?有人可以帮助我吗,我确定这是一个愚蠢的错误,但我不认识任何擅长 C# 的人。这是代码:
堆栈溢出和 vb/c# 很抱歉,如果有任何愚蠢的错误。
我也有尝试和捕获,但我删除了它们,希望它们是导致问题的原因,但事实并非如此。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
string date = DateTime.Now.ToString("ddMMyy");
string Source = @"G:\Personal\A1";
string Destination = @"D:\_Lil USB Backup\" + date;
if (System.IO.Directory.Exists(Source))
{
if (!System.IO.Directory.Exists(Destination))
{
System.IO.Directory.CreateDirectory(Destination);
}
CopyAllFiles(Source, Destination);
Console.WriteLine("Task Completed successfully.");
Thread.Sleep(5000);
}
else
{
Console.WriteLine("Source does not exist, try plugging the USB in dips**t.");
Thread.Sleep(5000);
}
}
private static void CopyAllFiles(string Source, string Destination)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(Source);
DirectoryInfo[] dirs = dir.GetDirectories();
// 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(Destination, file.Name);
file.CopyTo(temppath, true);
}
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(Destination, subdir.Name);
CopyAllFiles(subdir.FullName, temppath);
}
Console.WriteLine("Successfully copied files.");
}
}
输出到控制台:
成功复制文件。
成功复制文件。
成功复制文件。
任务成功完成。
控制台的预期输出:
成功复制文件。
任务成功完成。
(虽然文件复制正确)。
CopyAllFiles()
递归调用 CopyAllFiles()
,每次调用都会调用 WriteLine() 语句。所以,您看到的输出符合代码和我的期望。
我看到的最简单的解决方案是移动
Console.WriteLine("Successfully copied files.");
从 CopyAllFiles()
进入 Main()
函数。
我从多个在线资源创建了一个自动备份脚本,并且我对 C# 的了解有限,但出于某种原因,它重复了第一行写入?有人可以帮助我吗,我确定这是一个愚蠢的错误,但我不认识任何擅长 C# 的人。这是代码:
堆栈溢出和 vb/c# 很抱歉,如果有任何愚蠢的错误。
我也有尝试和捕获,但我删除了它们,希望它们是导致问题的原因,但事实并非如此。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
string date = DateTime.Now.ToString("ddMMyy");
string Source = @"G:\Personal\A1";
string Destination = @"D:\_Lil USB Backup\" + date;
if (System.IO.Directory.Exists(Source))
{
if (!System.IO.Directory.Exists(Destination))
{
System.IO.Directory.CreateDirectory(Destination);
}
CopyAllFiles(Source, Destination);
Console.WriteLine("Task Completed successfully.");
Thread.Sleep(5000);
}
else
{
Console.WriteLine("Source does not exist, try plugging the USB in dips**t.");
Thread.Sleep(5000);
}
}
private static void CopyAllFiles(string Source, string Destination)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(Source);
DirectoryInfo[] dirs = dir.GetDirectories();
// 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(Destination, file.Name);
file.CopyTo(temppath, true);
}
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(Destination, subdir.Name);
CopyAllFiles(subdir.FullName, temppath);
}
Console.WriteLine("Successfully copied files.");
}
}
输出到控制台: 成功复制文件。 成功复制文件。 成功复制文件。 任务成功完成。
控制台的预期输出: 成功复制文件。 任务成功完成。
(虽然文件复制正确)。
CopyAllFiles()
递归调用 CopyAllFiles()
,每次调用都会调用 WriteLine() 语句。所以,您看到的输出符合代码和我的期望。
我看到的最简单的解决方案是移动
Console.WriteLine("Successfully copied files.");
从 CopyAllFiles()
进入 Main()
函数。