使用数据库维护文件管理,修复文件管理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();
} }
fileInfo.CopyTo(desInfo.FullName,true); try
fileInfo.Delete(); {
file.NeedChange = false; fileInfo.CopyTo(desInfo.FullName, true);
Debug.WriteLine($"moved the file and marked {fileInfo.FullName} --> {desInfo.FullName}"); 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++; doneNum++;
} }
fileInfoDb.SaveChanges(); fileInfoDb.SaveChanges();

View File

@ -49,15 +49,34 @@ using System.Management;
}); });
var planPrint = Task.Run(() => var planPrint = Task.Run(() =>
{ {
Debug.WriteLine("scanning and get totle num"); try
var fileNumber = FileCopy.DicFileNum(Path.Join("D:", "老师文档"));
Debug.WriteLine("scanned ok and get totle num");
while (!copyTask.IsCompleted)
{ {
var usbRoot = new DirectoryInfo(driveName); Debug.WriteLine("scanning and get totle num");
if (usbRoot.GetFiles().Where(w => w.Name == $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd").ToArray().Length != 1) 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(); var oldPrint = usbRoot.GetFiles().Where(w => w.Name.EndsWith(".upd")).ToArray();
Debug.WriteLine(doneNum.ToString()); Debug.WriteLine(doneNum.ToString());
@ -65,26 +84,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();
Thread.Sleep(1000); }
} }catch(Exception ex)
{ {
var usbRoot = new DirectoryInfo(driveName); Debug.WriteLine(ex.ToString());
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();
} }
}); });
@ -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()}");*/