Я создал сценарий автоматического резервного копирования из нескольких онлайн-источников и моих собственных ограниченных знаний 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.");
}
}
Вывод на консоль:
Успешно скопированные файлы.
Успешно скопированные файлы.
Успешно скопированные файлы.
Задача выполнена успешно.
Ожидаемый вывод на консоль:
Успешно скопированные файлы.
Задача выполнена успешно.
(хотя файлы копируются правильно).