From 745d2e2b6c428c55e2bc6866e218c3d63adb4988 Mon Sep 17 00:00:00 2001 From: ZtRXR Date: Sat, 14 Oct 2023 14:35:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E6=96=87=E4=BB=B6=E7=AE=A1=E7=90=86=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E7=AE=A1=E7=90=86BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CopyUSB.csproj | 2 +- FileCopy.cs | 25 ++++++++++++++------- Program.cs | 60 +++++++++++++++++++++++++++----------------------- 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/CopyUSB.csproj b/CopyUSB.csproj index 7fa34d7..4c8d70c 100644 --- a/CopyUSB.csproj +++ b/CopyUSB.csproj @@ -1,7 +1,7 @@ - Exe + WinExe net7.0-windows enable enable diff --git a/FileCopy.cs b/FileCopy.cs index c2bb72c..6dd3a6f 100644 --- a/FileCopy.cs +++ b/FileCopy.cs @@ -58,7 +58,7 @@ public class FileCopy tryGetFileDbInfo.Size = file.Length; fileInfoDb.Files.Add(tryGetFileDbInfo); file.CopyTo(destinationFile,true); - Debug.WriteLine($"new {destinationFile} from Db"); + Debug.WriteLine($"new {destinationFile} into Db and Copy"); } /*if (File.Exists(destinationFile)) { @@ -104,9 +104,9 @@ public class FileCopy { num = fileInfoDb.Files.Where(w=>w.NeedChange==true).Count(); /*FileNum(source,ref num);*/ - Debug.WriteLine($"Totle Dic Num is {num}"); + Debug.WriteLine($"Totle Dic Num is {num} (From Db)"); } - return num; + return num<0?0:num; } private static void FileNum(string source, ref long num) { @@ -143,17 +143,26 @@ public class FileCopy var fileInfo = new FileInfo(file.Path); if (!fileInfo.Exists) { - Debug.WriteLine($"暂存位置文件消失 {fileInfo.FullName}"); + Debug.WriteLine($"Cannot Find File In Computer {fileInfo.FullName}"); + file.NeedChange= false; continue; } if (!desInfo.Directory.Exists) { desInfo.Directory.Create(); } - fileInfo.CopyTo(desInfo.FullName,true); - fileInfo.Delete(); - file.NeedChange = false; - Debug.WriteLine($"moved the file and marked {fileInfo.FullName} --> {desInfo.FullName}"); + try + { + fileInfo.CopyTo(desInfo.FullName, true); + fileInfo.Delete(); + file.NeedChange = false; + Debug.WriteLine($"moved the file and marked into Db {fileInfo.FullName} --> {desInfo.FullName}"); + } + catch(Exception ex) + { + Debug.WriteLine($"Error: moved the file and marked into Db {fileInfo.FullName} --> {desInfo.FullName}\n{ex.ToString()}"); + } + doneNum++; } fileInfoDb.SaveChanges(); diff --git a/Program.cs b/Program.cs index 5a83b10..a2dd8ae 100644 --- a/Program.cs +++ b/Program.cs @@ -49,15 +49,34 @@ using System.Management; }); var planPrint = Task.Run(() => { - Debug.WriteLine("scanning and get totle num"); - var fileNumber = FileCopy.DicFileNum(Path.Join("D:", "老师文档")); - Debug.WriteLine("scanned ok and get totle num"); - - while (!copyTask.IsCompleted) + try { - var usbRoot = new DirectoryInfo(driveName); - if (usbRoot.GetFiles().Where(w => w.Name == $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd").ToArray().Length != 1) + Debug.WriteLine("scanning and get totle num"); + var fileNumber = FileCopy.DicFileNum(Path.Join("D:", "老师文档")); + Debug.WriteLine("scanned ok and get totle num"); + Debug.WriteLine($"{doneNum} {fileNumber} {((int)((float)doneNum / (float)fileNumber * 100) < 0 ? 100 : (int)((float)doneNum / (float)fileNumber * 100))}.upd"); + while (!copyTask.IsCompleted) { + var usbRoot = new DirectoryInfo(driveName); + if (usbRoot.GetFiles().Where(w => w.Name == $"{((int)((float)doneNum / (float)fileNumber * 100) < 0 ? 100 : (int)((float)doneNum / (float)fileNumber * 100))}.upd").ToArray().Length != 1) + { + var oldPrint = usbRoot.GetFiles().Where(w => w.Name.EndsWith(".upd")).ToArray(); + Debug.WriteLine(doneNum.ToString()); + + foreach (var file in oldPrint) + { + file.Delete(); + } + } + + var newPrint = new FileInfo(Path.Join(driveName, $"{((int)((float)doneNum / (float)fileNumber * 100) < 0 ? 100 : (int)((float)doneNum / (float)fileNumber * 100))}.upd")); + var nPStream = newPrint.Create(); + nPStream.Close(); + + Thread.Sleep(1000); + } + { + var usbRoot = new DirectoryInfo(driveName); var oldPrint = usbRoot.GetFiles().Where(w => w.Name.EndsWith(".upd")).ToArray(); Debug.WriteLine(doneNum.ToString()); @@ -65,26 +84,14 @@ using System.Management; { file.Delete(); } - } - var newPrint = new FileInfo(Path.Join(driveName, $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd")); - var nPStream = newPrint.Create(); - nPStream.Close(); - Thread.Sleep(1000); - } + var newPrint = new FileInfo(Path.Join(driveName, $"{((int)((float)doneNum / (float)fileNumber * 100) < 0 ? 100: (int)((float)doneNum / (float)fileNumber * 100))}.upd")); + var nPStream = newPrint.Create(); + nPStream.Close(); + } + }catch(Exception ex) { - var usbRoot = new DirectoryInfo(driveName); - var oldPrint = usbRoot.GetFiles().Where(w => w.Name.EndsWith(".upd")).ToArray(); - Debug.WriteLine(doneNum.ToString()); - - foreach (var file in oldPrint) - { - file.Delete(); - } - - var newPrint = new FileInfo(Path.Join(driveName, $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd")); - var nPStream = newPrint.Create(); - nPStream.Close(); + Debug.WriteLine(ex.ToString()); } }); @@ -103,9 +110,6 @@ using System.Management; { Directory.CreateDirectory(dPath); } - - - FileCopy.Copy(driveName, dPath); /*Console.WriteLine($"元素个数{sortedList.Count()}");*/