CopyUSB/FileCopy.cs

70 lines
1.7 KiB
C#

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