2023-10-01 10:12:37 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CopyUSB;
|
2023-10-01 13:07:25 +00:00
|
|
|
|
public class CopyList
|
|
|
|
|
{
|
|
|
|
|
public string Func { get; set; }
|
|
|
|
|
public long length { get; set; }
|
|
|
|
|
public FileInfo file { get; set; }
|
|
|
|
|
public string des { get; set; }
|
|
|
|
|
}
|
2023-10-01 10:12:37 +00:00
|
|
|
|
public class FileCopy
|
|
|
|
|
{
|
2023-10-01 13:07:25 +00:00
|
|
|
|
|
|
|
|
|
public static void Copy(string source,string des, List<(long, FileInfo, string, string)> copyList)
|
2023-10-01 10:12:37 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var sourceDir = new DirectoryInfo(source);
|
|
|
|
|
var desDir = new DirectoryInfo(des);
|
|
|
|
|
|
|
|
|
|
// 确保目标目录存在
|
|
|
|
|
if (!desDir.Exists)
|
|
|
|
|
{
|
|
|
|
|
desDir.Create();
|
|
|
|
|
}
|
|
|
|
|
//遍历目录
|
|
|
|
|
foreach (var subDir in sourceDir.GetDirectories())
|
|
|
|
|
{
|
2023-10-01 13:07:25 +00:00
|
|
|
|
Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name), copyList);
|
2023-10-01 10:12:37 +00:00
|
|
|
|
}
|
|
|
|
|
foreach (var file in sourceDir.GetFiles())
|
|
|
|
|
{
|
|
|
|
|
string destinationFile = Path.Combine(desDir.FullName, file.Name);
|
2023-10-01 10:56:34 +00:00
|
|
|
|
if (File.Exists(destinationFile))
|
|
|
|
|
{
|
|
|
|
|
var desFileInfo = new FileInfo(destinationFile);
|
|
|
|
|
if (desFileInfo.Length != file.Length)
|
|
|
|
|
{
|
2023-10-01 13:07:25 +00:00
|
|
|
|
/*Console.WriteLine($"Renew {file} --> {destinationFile}");*/
|
|
|
|
|
copyList.Add((file.Length,file, destinationFile,"Renew"));
|
|
|
|
|
/*file.CopyTo(destinationFile, true);*/
|
2023-10-01 10:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-01 13:07:25 +00:00
|
|
|
|
Console.WriteLine($"Same {file} --> {destinationFile} {file.Length/1024/1024}MB");
|
|
|
|
|
/*copyList.Add((file, destinationFile, 0, "Same"));*/
|
2023-10-01 10:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-01 13:07:25 +00:00
|
|
|
|
/*Console.WriteLine($"NewFile {file} --> {destinationFile}");*/
|
|
|
|
|
copyList.Add((file.Length,file, destinationFile, "NewFile"));
|
2023-10-01 10:56:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-01 10:12:37 +00:00
|
|
|
|
}
|
2023-10-01 13:07:25 +00:00
|
|
|
|
|
|
|
|
|
|
2023-10-01 10:12:37 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-10-01 13:07:25 +00:00
|
|
|
|
Console.WriteLine($"Copy Error {source}");
|
2023-10-01 10:12:37 +00:00
|
|
|
|
/*Console.WriteLine(ex.ToString());*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|