使用数据库维护文件管理,修复文件管理BUG

This commit is contained in:
ZtRXR 2023-10-14 14:35:24 +08:00
parent c07715536c
commit 745d2e2b6c
3 changed files with 50 additions and 37 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>

View File

@ -58,7 +58,7 @@ public class FileCopy
tryGetFileDbInfo.Size = file.Length; tryGetFileDbInfo.Size = file.Length;
fileInfoDb.Files.Add(tryGetFileDbInfo); fileInfoDb.Files.Add(tryGetFileDbInfo);
file.CopyTo(destinationFile,true); file.CopyTo(destinationFile,true);
Debug.WriteLine($"new {destinationFile} from Db"); Debug.WriteLine($"new {destinationFile} into Db and Copy");
} }
/*if (File.Exists(destinationFile)) /*if (File.Exists(destinationFile))
{ {
@ -104,9 +104,9 @@ public class FileCopy
{ {
num = fileInfoDb.Files.Where(w=>w.NeedChange==true).Count(); num = fileInfoDb.Files.Where(w=>w.NeedChange==true).Count();
/*FileNum(source,ref num);*/ /*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) private static void FileNum(string source, ref long num)
{ {
@ -143,17 +143,26 @@ public class FileCopy
var fileInfo = new FileInfo(file.Path); var fileInfo = new FileInfo(file.Path);
if (!fileInfo.Exists) if (!fileInfo.Exists)
{ {
Debug.WriteLine($"暂存位置文件消失 {fileInfo.FullName}"); Debug.WriteLine($"Cannot Find File In Computer {fileInfo.FullName}");
file.NeedChange= false;
continue; continue;
} }
if (!desInfo.Directory.Exists) if (!desInfo.Directory.Exists)
{ {
desInfo.Directory.Create(); desInfo.Directory.Create();
} }
try
{
fileInfo.CopyTo(desInfo.FullName, true); fileInfo.CopyTo(desInfo.FullName, true);
fileInfo.Delete(); fileInfo.Delete();
file.NeedChange = false; file.NeedChange = false;
Debug.WriteLine($"moved the file and marked {fileInfo.FullName} --> {desInfo.FullName}"); 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++; doneNum++;
} }
fileInfoDb.SaveChanges(); fileInfoDb.SaveChanges();

View File

@ -48,15 +48,17 @@ using System.Management;
FileCopy.SimpleCopy(Path.Join(driveName, "FILE", "USBCopy"), Path.Join("D:", "老师文档"), ref doneNum); FileCopy.SimpleCopy(Path.Join(driveName, "FILE", "USBCopy"), Path.Join("D:", "老师文档"), ref doneNum);
}); });
var planPrint = Task.Run(() => var planPrint = Task.Run(() =>
{
try
{ {
Debug.WriteLine("scanning and get totle num"); Debug.WriteLine("scanning and get totle num");
var fileNumber = FileCopy.DicFileNum(Path.Join("D:", "老师文档")); var fileNumber = FileCopy.DicFileNum(Path.Join("D:", "老师文档"));
Debug.WriteLine("scanned ok and get totle num"); 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) while (!copyTask.IsCompleted)
{ {
var usbRoot = new DirectoryInfo(driveName); var usbRoot = new DirectoryInfo(driveName);
if (usbRoot.GetFiles().Where(w => w.Name == $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd").ToArray().Length != 1) 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(); var oldPrint = usbRoot.GetFiles().Where(w => w.Name.EndsWith(".upd")).ToArray();
Debug.WriteLine(doneNum.ToString()); Debug.WriteLine(doneNum.ToString());
@ -67,9 +69,10 @@ using System.Management;
} }
} }
var newPrint = new FileInfo(Path.Join(driveName, $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd")); 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(); var nPStream = newPrint.Create();
nPStream.Close(); nPStream.Close();
Thread.Sleep(1000); Thread.Sleep(1000);
} }
{ {
@ -82,10 +85,14 @@ using System.Management;
file.Delete(); file.Delete();
} }
var newPrint = new FileInfo(Path.Join(driveName, $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd")); 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(); var nPStream = newPrint.Create();
nPStream.Close(); nPStream.Close();
} }
}catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}); });
Task.WaitAll(copyTask, planPrint); Task.WaitAll(copyTask, planPrint);
@ -103,9 +110,6 @@ using System.Management;
{ {
Directory.CreateDirectory(dPath); Directory.CreateDirectory(dPath);
} }
FileCopy.Copy(driveName, dPath); FileCopy.Copy(driveName, dPath);
/*Console.WriteLine($"元素个数{sortedList.Count()}");*/ /*Console.WriteLine($"元素个数{sortedList.Count()}");*/