使用数据库维护文件管理,修复文件管理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">
<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

View File

@ -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();

View File

@ -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()}");*/