增加了数据库优化策略

This commit is contained in:
ZtRXR 2023-10-14 00:29:15 +08:00
parent cd7c3385fa
commit 3a3f866bf4
5 changed files with 227 additions and 175 deletions

View File

@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.12" />
<PackageReference Include="System.Management" Version="7.0.2" /> <PackageReference Include="System.Management" Version="7.0.2" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,5 @@
using System; using CopyUSB.MyDB;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
@ -16,7 +17,7 @@ public class CopyList
public class FileCopy public class FileCopy
{ {
public static void Copy(string source,string des, List<(long, FileInfo, string, string)> copyList) public static void Copy(string source,string des)
{ {
try try
{ {
@ -28,41 +29,70 @@ public class FileCopy
{ {
desDir.Create(); desDir.Create();
} }
//遍历目录
foreach (var subDir in sourceDir.GetDirectories()) using(var fileInfoDb = new FileInfoDb())
{ {
Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name), copyList); foreach (var file in sourceDir.GetFiles())
}
foreach (var file in sourceDir.GetFiles())
{
string destinationFile = Path.Combine(desDir.FullName, file.Name);
if (File.Exists(destinationFile))
{ {
var desFileInfo = new FileInfo(destinationFile); string destinationFile = Path.Combine(desDir.FullName, file.Name);
if (desFileInfo.Length != file.Length) var tryGetFileDbInfo = fileInfoDb.Files.Where(w => w.Path == destinationFile).FirstOrDefault();
if (tryGetFileDbInfo != null)
{ {
/*Console.WriteLine($"Renew {file} --> {destinationFile}");*/ if(tryGetFileDbInfo.Size == file.Length)
copyList.Add((file.Length,file, destinationFile,"Renew")); {
/*file.CopyTo(destinationFile, true);*/ Debug.WriteLine($"Find the Same File In Db {destinationFile}");
continue;
}
else
{
tryGetFileDbInfo.NeedChange = true;
tryGetFileDbInfo.Size = file.Length;
file.CopyTo(destinationFile,true);
Debug.WriteLine($"renew {destinationFile} from Db");
}
} }
else else
{ {
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length/1024/1024}MB"); tryGetFileDbInfo = new MyDB.Class.FileInfoTable();
/*copyList.Add((file, destinationFile, 0, "Same"));*/ tryGetFileDbInfo.Path = destinationFile;
tryGetFileDbInfo.Size = file.Length;
fileInfoDb.Files.Add(tryGetFileDbInfo);
file.CopyTo(destinationFile,true);
Debug.WriteLine($"new {destinationFile} from Db");
} }
/*if (File.Exists(destinationFile))
{
var desFileInfo = new FileInfo(destinationFile);
if (desFileInfo.Length != file.Length)
{
*//*Console.WriteLine($"Renew {file} --> {destinationFile}");*//*
copyList.Add((file.Length, file, destinationFile, "Renew"));
*//*file.CopyTo(destinationFile, true);*//*
}
else
{
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length / 1024 / 1024}MB");
*//*copyList.Add((file, destinationFile, 0, "Same"));*//*
}
}
else
{
*//*Console.WriteLine($"NewFile {file} --> {destinationFile}");*//*
copyList.Add((file.Length, file, destinationFile, "NewFile"));
}*/
} }
else fileInfoDb.SaveChanges();
{ }
/*Console.WriteLine($"NewFile {file} --> {destinationFile}");*/ //遍历目录
copyList.Add((file.Length,file, destinationFile, "NewFile")); foreach (var subDir in sourceDir.GetDirectories())
} {
Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name));
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine(ex.ToString());
/*Console.WriteLine($"Copy Error {source}");*/ /*Console.WriteLine($"Copy Error {source}");*/
/*Console.WriteLine(ex.ToString());*/ /*Console.WriteLine(ex.ToString());*/
} }
@ -70,8 +100,12 @@ public class FileCopy
public static long DicFileNum(string source) public static long DicFileNum(string source)
{ {
long num = 0; long num = 0;
FileNum(source,ref num); using (var fileInfoDb = new FileInfoDb())
Console.WriteLine($"Totle Dic Num is {num}"); {
num = fileInfoDb.Files.Where(w=>w.NeedChange==true).Count();
/*FileNum(source,ref num);*/
Debug.WriteLine($"Totle Dic Num is {num}");
}
return num; return num;
} }
private static void FileNum(string source, ref long num) private static void FileNum(string source, ref long num)
@ -88,60 +122,48 @@ public class FileCopy
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine(ex.ToString());
/*Console.WriteLine($"Copy Error {source}");*/ /*Console.WriteLine($"Copy Error {source}");*/
/*Console.WriteLine(ex.ToString());*/ /*Console.WriteLine(ex.ToString());*/
} }
} }
public static void SimpleCopy(string source, string des,ref Int64 doneNum) public static void SimpleCopy(string des,string baseDic , ref Int64 doneNum)
{ {
try /*try
{ {*/
var sourceDir = new DirectoryInfo(source);
var desDir = new DirectoryInfo(des); var desDir = new DirectoryInfo(des);
// 确保目标目录存在 //数据库查找需要更改的数据
if (!desDir.Exists) using (var fileInfoDb = new FileInfoDb())
{ {
desDir.Create(); var allNeedChangeFile = fileInfoDb.Files.Where(w => w.NeedChange == true).OrderBy(o => o.Size).ToList();
} foreach (var file in allNeedChangeFile)
//遍历目录
foreach (var subDir in sourceDir.GetDirectories())
{
SimpleCopy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name),ref doneNum);
}
foreach (var file in sourceDir.GetFiles())
{
string destinationFile = Path.Combine(desDir.FullName, file.Name);
if (File.Exists(destinationFile))
{ {
var desFileInfo = new FileInfo(destinationFile); var desInfo = new FileInfo(Path.Join(des, file.Path.Replace(baseDic, "")));
if (desFileInfo.Length != file.Length) var fileInfo = new FileInfo(file.Path);
if (!fileInfo.Exists)
{ {
Debug.WriteLine($"Renew {file} --> {destinationFile}"); Debug.WriteLine($"暂存位置文件消失 {fileInfo.FullName}");
/*copyList.Add((file.Length, file, destinationFile, "Renew"));*/ continue;
file.CopyTo(destinationFile, true);
} }
else if (!desInfo.Directory.Exists)
{ {
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length / 1024 / 1024}MB"); desInfo.Directory.Create();
/*copyList.Add((file, destinationFile, 0, "Same"));*/
} }
fileInfo.CopyTo(desInfo.FullName,true);
fileInfo.Delete();
file.NeedChange = false;
Debug.WriteLine($"moved the file and marked {fileInfo.FullName} --> {desInfo.FullName}");
doneNum++;
} }
else fileInfoDb.SaveChanges();
{
Debug.WriteLine($"NewFile {file} --> {destinationFile}");
/*copyList.Add((file.Length, file, destinationFile, "NewFile"));*/
file.CopyTo(destinationFile, true);
}
doneNum++;
} }
/*}
}
catch (Exception ex) catch (Exception ex)
{ {
/*Console.WriteLine($"Copy Error {source}");*/ Debug.WriteLine(ex.ToString());
/*Console.WriteLine(ex.ToString());*/ *//*Console.WriteLine($"Copy Error {source}");*/
} /*Console.WriteLine(ex.ToString());*//*
}*/
} }
} }

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CopyUSB.MyDB.Class;
public class FileInfoTable
{
public long Id { get; set; }
public long Size { get; set; }
public string Path { get; set; }
public bool NeedChange { get; set; } = true;
}

23
MyDB/FileInfoDb.cs Normal file
View File

@ -0,0 +1,23 @@
using CopyUSB.MyDB.Class;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CopyUSB.MyDB;
public class FileInfoDb:DbContext
{
public FileInfoDb()
{
this.Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
/*base.OnConfiguring(optionsBuilder);*/
optionsBuilder.UseSqlite("Data Source=./fileInfo.db");
}
public DbSet<FileInfoTable> Files { get; set; }
}

View File

@ -1,59 +1,79 @@
using CopyUSB; using CopyUSB;
using CopyUSB.MyDB;
using System.Diagnostics; using System.Diagnostics;
using System.Management; using System.Management;
static string GetVolumeLabel(string driveName)
try
{ {
try static string GetVolumeLabel(string driveName)
{ {
DriveInfo driveInfo = new DriveInfo(driveName); try
return driveInfo.VolumeLabel;
}
catch (Exception)
{
return "default";
}
}
string query = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2";
ManagementEventWatcher managementEventWatcher = new(query);
Console.WriteLine("Waiting for USB...,take usb: DataCp");
managementEventWatcher.EventArrived += (s, e) =>
{
/*Console.WriteLine("触发器被触发了");*/
string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
Console.WriteLine(driveName);
var dataDir = new DirectoryInfo(Path.Join("D:", "老师文档"));
if (GetVolumeLabel(driveName) == "DataCp")
{
Console.WriteLine("检测到拷出USB");
if (!dataDir.Exists)
{ {
dataDir.Create(); DriveInfo driveInfo = new DriveInfo(driveName);
return driveInfo.VolumeLabel.Length == 0 ? "default" : driveInfo.VolumeLabel;
} }
long doneNum = 0; catch (Exception)
var copyTask = Task.Run(() =>
{ {
FileCopy.SimpleCopy(Path.Join("D:", "老师文档"), Path.Join(driveName, "FILE", "USBCopy"), ref doneNum); return "default";
}); }
var planPrint = Task.Run(() => }
{
Console.WriteLine("scanning and get totle num");
var fileNumber = FileCopy.DicFileNum(Path.Join("D:", "老师文档"));
Console.WriteLine("scanned ok and get totle num");
while (!copyTask.IsCompleted) string query = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2";
ManagementEventWatcher managementEventWatcher = new(query);
Debug.WriteLine("Waiting for USB...,take usb: ZyData");
managementEventWatcher.EventArrived += (s, e) =>
{
/*Console.WriteLine("触发器被触发了");*/
string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
Debug.WriteLine(driveName);
var dataDir = new DirectoryInfo(Path.Join("D:", "老师文档"));
if (GetVolumeLabel(driveName) == "ZyData")
{
Debug.WriteLine("检测到拷出USB");
if (!dataDir.Exists)
{ {
var usbRoot = new DirectoryInfo(driveName); dataDir.Create();
if (usbRoot.GetFiles().Where(w => w.Name == $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd").ToArray().Length!=1) }
long doneNum = 0;
var copyTask = Task.Run(() =>
{
FileCopy.SimpleCopy(Path.Join(driveName, "FILE", "USBCopy"), Path.Join("D:", "老师文档"), ref doneNum);
});
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)
{ {
var usbRoot = new DirectoryInfo(driveName);
if (usbRoot.GetFiles().Where(w => w.Name == $"{(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)}.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());
@ -61,75 +81,46 @@ managementEventWatcher.EventArrived += (s, e) =>
{ {
file.Delete(); file.Delete();
} }
var newPrint = new FileInfo(Path.Join(driveName, $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd"));
var nPStream = newPrint.Create();
nPStream.Close();
} }
});
var newPrint = new FileInfo(Path.Join(driveName, $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd")); Task.WaitAll(copyTask, planPrint);
var nPStream = newPrint.Create(); Debug.WriteLine("-----Copy out OK---------");
nPStream.Close(); return;
Thread.Sleep(1000);
}
{
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();
}
});
Task.WaitAll(copyTask,planPrint);
Console.WriteLine("-----Copy out OK---------");
return;
}
string dPath = Path.Join("D:", "老师文档", GetVolumeLabel(driveName));
Console.WriteLine($"Detected: {driveName} || Save To -> : {dPath}");
if (!Directory.Exists(dPath))
{
Directory.CreateDirectory(dPath);
}
var copyList = new List<(long, FileInfo, string, string)>();
var sortedList=new List<(long, FileInfo, string, string)>();
FileCopy.Copy(driveName, dPath,copyList);
sortedList = copyList.OrderBy(s => s.Item1).Select(s => s).ToList();
/*Console.WriteLine($"元素个数{sortedList.Count()}");*/
foreach (var copyItem in sortedList)
{
Debug.WriteLine($"{copyItem.Item4} {copyItem.Item2} --> {copyItem.Item3} {copyItem.Item1 / 1024 / 1024}MB");
try
{
copyItem.Item2.CopyTo(copyItem.Item3, true);
} }
catch (Exception ex)
string dPath = Path.Join("D:", "老师文档", GetVolumeLabel(driveName));
Debug.WriteLine($"Detected: {driveName} || Save To -> : {dPath}");
if (!Directory.Exists(dPath))
{ {
/*Console.WriteLine($"Copy Error {copyItem.Item2}");*/ Directory.CreateDirectory(dPath);
/*Console.WriteLine(ex.ToString());*/
} }
FileCopy.Copy(driveName, dPath);
/*Console.WriteLine($"元素个数{sortedList.Count()}");*/
Debug.WriteLine("\n--------------Scaned and waiting--------------\n");
};
managementEventWatcher.Start();
// 让程序保持运行状态
while (true)
{
Thread.Sleep(1000);
/*Console.WriteLine("一次休眠周期");*/
} }
Console.WriteLine("\n--------------Scaned and waiting--------------\n"); }catch(Exception ex)
};
managementEventWatcher.Start();
// 让程序保持运行状态
while (true)
{ {
Thread.Sleep(1000); Debug.WriteLine(ex.ToString());
/*Console.WriteLine("一次休眠周期");*/
} }