从大到小复制逻辑
This commit is contained in:
parent
872644e47e
commit
cbb5b5588b
93
FileCopy.cs
93
FileCopy.cs
@ -16,6 +16,30 @@ public class CopyList
|
||||
}
|
||||
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)
|
||||
{
|
||||
@ -29,72 +53,51 @@ public class FileCopy
|
||||
{
|
||||
desDir.Create();
|
||||
}
|
||||
|
||||
using(var fileInfoDb = new FileInfoDb())
|
||||
var cpFileList = new List<(FileInfo, FileInfo)>();
|
||||
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
|
||||
{
|
||||
string destinationFile = Path.Combine(desDir.FullName, file.Name);
|
||||
var tryGetFileDbInfo = fileInfoDb.Files.Where(w => w.Path == destinationFile).FirstOrDefault();
|
||||
if (tryGetFileDbInfo != null)
|
||||
var tryGetFileDb = fileInfoDb.Files.Where(w=>w.Path==file.Item2.FullName).FirstOrDefault();
|
||||
if (tryGetFileDb == 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}");
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
tryGetFileDbInfo.NeedChange = true;
|
||||
tryGetFileDbInfo.Size = file.Length;
|
||||
file.CopyTo(destinationFile, true);
|
||||
Debug.WriteLine($"renew {destinationFile} from Db");
|
||||
file.Item2.Directory.Create();
|
||||
}
|
||||
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
|
||||
{
|
||||
tryGetFileDbInfo = new MyDB.Class.FileInfoTable();
|
||||
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)
|
||||
if(tryGetFileDb.Size == file.Item1.Length)
|
||||
{
|
||||
*//*Console.WriteLine($"Renew {file} --> {destinationFile}");*//*
|
||||
copyList.Add((file.Length, file, destinationFile, "Renew"));
|
||||
*//*file.CopyTo(destinationFile, true);*//*
|
||||
Debug.WriteLine($"Found Same file from db {file.Item1.FullName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length / 1024 / 1024}MB");
|
||||
*//*copyList.Add((file, destinationFile, 0, "Same"));*//*
|
||||
file.Item1.CopyTo (file.Item2.FullName,true);
|
||||
tryGetFileDb.Size = (int)file.Item1.Length;
|
||||
tryGetFileDb.NeedChange= true;
|
||||
Debug.WriteLine($"Renew {file.Item1.FullName}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
*//*Console.WriteLine($"NewFile {file} --> {destinationFile}");*//*
|
||||
copyList.Add((file.Length, file, destinationFile, "NewFile"));
|
||||
}*/
|
||||
|
||||
}catch(Exception ex)
|
||||
}catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex.ToString());
|
||||
Debug.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
fileInfoDb.SaveChanges();
|
||||
}
|
||||
//遍历目录
|
||||
foreach (var subDir in sourceDir.GetDirectories())
|
||||
{
|
||||
Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
11
Program.cs
11
Program.cs
@ -20,7 +20,7 @@ using System.Management;
|
||||
|
||||
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);
|
||||
|
||||
var dataDir = new DirectoryInfo(Path.Join("D:", "老师文档"));
|
||||
|
||||
if (GetVolumeLabel(driveName) == "ZyData")
|
||||
if (GetVolumeLabel(driveName) == "RXRData")
|
||||
{
|
||||
Debug.WriteLine("检测到拷出USB");
|
||||
if (!dataDir.Exists)
|
||||
@ -99,7 +100,11 @@ using System.Management;
|
||||
Debug.WriteLine("-----Copy out OK---------");
|
||||
return;
|
||||
}
|
||||
|
||||
if (GetVolumeLabel(driveName).StartsWith("RXR"))
|
||||
{
|
||||
Debug.WriteLine("-----It's my USB-----");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
string dPath = Path.Join("D:", "老师文档", GetVolumeLabel(driveName));
|
||||
|
Loading…
Reference in New Issue
Block a user