148 lines
3.6 KiB
C#
148 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
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
|
|
{
|
|
Debug.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());*/
|
|
}
|
|
}
|
|
public static long DicFileNum(string source)
|
|
{
|
|
long num = 0;
|
|
FileNum(source,ref num);
|
|
Console.WriteLine($"Totle Dic Num is {num}");
|
|
return num;
|
|
}
|
|
private static void FileNum(string source, ref long num)
|
|
{
|
|
try
|
|
{
|
|
var sourceDir = new DirectoryInfo(source);
|
|
num += sourceDir.GetFiles().Length;
|
|
//遍历目录
|
|
foreach (var subDir in sourceDir.GetDirectories())
|
|
{
|
|
FileNum(subDir.FullName,ref num);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
/*Console.WriteLine($"Copy Error {source}");*/
|
|
/*Console.WriteLine(ex.ToString());*/
|
|
}
|
|
}
|
|
public static void SimpleCopy(string source, string des,ref Int64 doneNum)
|
|
{
|
|
try
|
|
{
|
|
var sourceDir = new DirectoryInfo(source);
|
|
var desDir = new DirectoryInfo(des);
|
|
|
|
// 确保目标目录存在
|
|
if (!desDir.Exists)
|
|
{
|
|
desDir.Create();
|
|
}
|
|
//遍历目录
|
|
foreach (var subDir in sourceDir.GetDirectories())
|
|
{
|
|
SimpleCopy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name),ref doneNum);
|
|
}
|
|
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)
|
|
{
|
|
Debug.WriteLine($"Renew {file} --> {destinationFile}");
|
|
/*copyList.Add((file.Length, file, destinationFile, "Renew"));*/
|
|
file.CopyTo(destinationFile, true);
|
|
}
|
|
else
|
|
{
|
|
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length / 1024 / 1024}MB");
|
|
/*copyList.Add((file, destinationFile, 0, "Same"));*/
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.WriteLine($"NewFile {file} --> {destinationFile}");
|
|
/*copyList.Add((file.Length, file, destinationFile, "NewFile"));*/
|
|
file.CopyTo(destinationFile, true);
|
|
}
|
|
doneNum++;
|
|
}
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
/*Console.WriteLine($"Copy Error {source}");*/
|
|
/*Console.WriteLine(ex.ToString());*/
|
|
}
|
|
}
|
|
}
|