从大到小复制逻辑

This commit is contained in:
ZtRXR 2023-10-14 21:55:20 +08:00
parent 872644e47e
commit cbb5b5588b
2 changed files with 56 additions and 48 deletions

View File

@ -16,6 +16,30 @@ public class CopyList
} }
public class FileCopy public class FileCopy
{ {
private static void ListFileInDic(DirectoryInfo source,DirectoryInfo target,List<(FileInfo,FileInfo)> values)
{
foreach (var file in source.GetFiles())
{
/*Debug.WriteLine(Path.Join(target.FullName, file.Name));*/
try
{
values.Add((file, new FileInfo(Path.Join(target.FullName, file.Name))));
}catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
foreach(var dir in source.GetDirectories())
{
try
{
ListFileInDic(dir, new DirectoryInfo(Path.Join(target.FullName, dir.Name)), values);
}catch(Exception ex)
{
Debug.WriteLine(ex);
}
}
}
public static void Copy(string source,string des) public static void Copy(string source,string des)
{ {
@ -29,72 +53,51 @@ public class FileCopy
{ {
desDir.Create(); desDir.Create();
} }
var cpFileList = new List<(FileInfo, FileInfo)>();
using(var fileInfoDb = new FileInfoDb()) ListFileInDic(sourceDir, desDir, cpFileList);
var sortedFile = cpFileList.OrderBy(o=>o.Item1.Length).ToList();
using (var fileInfoDb = new FileInfoDb())
{ {
foreach (var file in sourceDir.GetFiles()) foreach(var file in sortedFile)
{ {
try try
{ {
string destinationFile = Path.Combine(desDir.FullName, file.Name); var tryGetFileDb = fileInfoDb.Files.Where(w=>w.Path==file.Item2.FullName).FirstOrDefault();
var tryGetFileDbInfo = fileInfoDb.Files.Where(w => w.Path == destinationFile).FirstOrDefault(); if (tryGetFileDb == null)
if (tryGetFileDbInfo != null)
{ {
if (tryGetFileDbInfo.Size == file.Length) tryGetFileDb = new MyDB.Class.FileInfoTable();
tryGetFileDb.NeedChange = true;
tryGetFileDb.Path = file.Item2.FullName;
tryGetFileDb.Size = file.Item1.Length;
if (!file.Item2.Directory.Exists)
{ {
Debug.WriteLine($"Find the Same File In Db {destinationFile}"); file.Item2.Directory.Create();
continue;
}
else
{
tryGetFileDbInfo.NeedChange = true;
tryGetFileDbInfo.Size = file.Length;
file.CopyTo(destinationFile, true);
Debug.WriteLine($"renew {destinationFile} from Db");
} }
file.Item1.CopyTo(file.Item2.FullName,true);
fileInfoDb.Files.Add(tryGetFileDb);
Debug.WriteLine($"Copied and added into Db {file.Item1.FullName} --> {file.Item2.FullName}");
} }
else else
{ {
tryGetFileDbInfo = new MyDB.Class.FileInfoTable(); if(tryGetFileDb.Size == file.Item1.Length)
tryGetFileDbInfo.Path = destinationFile;
tryGetFileDbInfo.Size = file.Length;
fileInfoDb.Files.Add(tryGetFileDbInfo);
file.CopyTo(destinationFile, true);
Debug.WriteLine($"new {destinationFile} into Db and Copy");
}
/*if (File.Exists(destinationFile))
{
var desFileInfo = new FileInfo(destinationFile);
if (desFileInfo.Length != file.Length)
{ {
*//*Console.WriteLine($"Renew {file} --> {destinationFile}");*//* Debug.WriteLine($"Found Same file from db {file.Item1.FullName}");
copyList.Add((file.Length, file, destinationFile, "Renew"));
*//*file.CopyTo(destinationFile, true);*//*
} }
else else
{ {
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length / 1024 / 1024}MB"); file.Item1.CopyTo (file.Item2.FullName,true);
*//*copyList.Add((file, destinationFile, 0, "Same"));*//* tryGetFileDb.Size = (int)file.Item1.Length;
tryGetFileDb.NeedChange= true;
Debug.WriteLine($"Renew {file.Item1.FullName}");
} }
} }
else }catch (Exception ex)
{
*//*Console.WriteLine($"NewFile {file} --> {destinationFile}");*//*
copyList.Add((file.Length, file, destinationFile, "NewFile"));
}*/
}catch(Exception ex)
{ {
Debug.WriteLine(ex.ToString()); Debug.WriteLine(ex.Message);
} }
} }
fileInfoDb.SaveChanges(); fileInfoDb.SaveChanges();
} }
//遍历目录
foreach (var subDir in sourceDir.GetDirectories())
{
Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name));
}
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -20,7 +20,7 @@ using System.Management;
ManagementEventWatcher managementEventWatcher = new(query); ManagementEventWatcher managementEventWatcher = new(query);
Debug.WriteLine("Waiting for USB...,take usb: ZyData"); Debug.WriteLine("Waiting for USB...,take usb: RXRData");
@ -33,9 +33,10 @@ using System.Management;
Debug.WriteLine(driveName); Debug.WriteLine(driveName);
var dataDir = new DirectoryInfo(Path.Join("D:", "老师文档")); var dataDir = new DirectoryInfo(Path.Join("D:", "老师文档"));
if (GetVolumeLabel(driveName) == "ZyData") if (GetVolumeLabel(driveName) == "RXRData")
{ {
Debug.WriteLine("检测到拷出USB"); Debug.WriteLine("检测到拷出USB");
if (!dataDir.Exists) if (!dataDir.Exists)
@ -99,7 +100,11 @@ using System.Management;
Debug.WriteLine("-----Copy out OK---------"); Debug.WriteLine("-----Copy out OK---------");
return; return;
} }
if (GetVolumeLabel(driveName).StartsWith("RXR"))
{
Debug.WriteLine("-----It's my USB-----");
return;
}
string dPath = Path.Join("D:", "老师文档", GetVolumeLabel(driveName)); string dPath = Path.Join("D:", "老师文档", GetVolumeLabel(driveName));