从小到大复制

This commit is contained in:
ZtRXR 2023-10-01 21:07:25 +08:00
parent 2d08e4d8e9
commit c4cd0e729c
2 changed files with 41 additions and 10 deletions

View File

@ -5,10 +5,17 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace CopyUSB; 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 class FileCopy
{ {
public static void Copy(string source,string des)
public static void Copy(string source,string des, List<(long, FileInfo, string, string)> copyList)
{ {
try try
{ {
@ -23,7 +30,7 @@ public class FileCopy
//遍历目录 //遍历目录
foreach (var subDir in sourceDir.GetDirectories()) foreach (var subDir in sourceDir.GetDirectories())
{ {
Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name)); Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name), copyList);
} }
foreach (var file in sourceDir.GetFiles()) foreach (var file in sourceDir.GetFiles())
{ {
@ -33,25 +40,29 @@ public class FileCopy
var desFileInfo = new FileInfo(destinationFile); var desFileInfo = new FileInfo(destinationFile);
if (desFileInfo.Length != file.Length) if (desFileInfo.Length != file.Length)
{ {
Console.WriteLine($"Renew {file} --> {destinationFile}"); /*Console.WriteLine($"Renew {file} --> {destinationFile}");*/
file.CopyTo(destinationFile, true); copyList.Add((file.Length,file, destinationFile,"Renew"));
/*file.CopyTo(destinationFile, true);*/
} }
else else
{ {
Console.WriteLine($"Same {file} --> {destinationFile}"); Console.WriteLine($"Same {file} --> {destinationFile} {file.Length/1024/1024}MB");
/*copyList.Add((file, destinationFile, 0, "Same"));*/
} }
} }
else else
{ {
Console.WriteLine($"NewFile {file} --> {destinationFile}"); /*Console.WriteLine($"NewFile {file} --> {destinationFile}");*/
file.CopyTo(destinationFile, true); copyList.Add((file.Length,file, destinationFile, "NewFile"));
} }
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"{source} Copy Error"); Console.WriteLine($"Copy Error {source}");
/*Console.WriteLine(ex.ToString());*/ /*Console.WriteLine(ex.ToString());*/
} }
} }

View File

@ -22,6 +22,7 @@ Console.WriteLine("Waiting for USB...");
managementEventWatcher.EventArrived += (s, e) => managementEventWatcher.EventArrived += (s, e) =>
{ {
/*Console.WriteLine("触发器被触发了");*/
string driveName = e.NewEvent.Properties["DriveName"].Value.ToString(); string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
string dPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "USBCopy", GetVolumeLabel(driveName)); string dPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "USBCopy", GetVolumeLabel(driveName));
@ -32,7 +33,26 @@ managementEventWatcher.EventArrived += (s, e) =>
{ {
Directory.CreateDirectory(dPath); Directory.CreateDirectory(dPath);
} }
FileCopy.Copy(driveName, dPath);
var copyList = new List<(long, FileInfo, string, string)>();
FileCopy.Copy(driveName, dPath,copyList);
var sortedList = copyList.OrderBy(s => s.Item1).Select(s => s);
/*Console.WriteLine($"元素个数{sortedList.Count()}");*/
foreach (var copyItem in sortedList)
{
Console.WriteLine($"{copyItem.Item4} {copyItem.Item2} --> {copyItem.Item3} {copyItem.Item1 / 1024 / 1024}MB");
try
{
copyItem.Item2.CopyTo(copyItem.Item3, true);
}
catch (Exception ex)
{
Console.WriteLine($"Copy Error {copyItem.Item2}");
/*Console.WriteLine(ex.ToString());*/
}
}
Console.WriteLine("\n--------------Scaned and waiting--------------\n"); Console.WriteLine("\n--------------Scaned and waiting--------------\n");
}; };