增加了数据库优化策略

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">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.12" />
<PackageReference Include="System.Management" Version="7.0.2" />
</ItemGroup>

View File

@ -1,4 +1,5 @@
using System;
using CopyUSB.MyDB;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@ -16,7 +17,7 @@ public class CopyList
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
{
@ -28,41 +29,70 @@ public class FileCopy
{
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())
{
string destinationFile = Path.Combine(desDir.FullName, file.Name);
if (File.Exists(destinationFile))
var tryGetFileDbInfo = fileInfoDb.Files.Where(w => w.Path == destinationFile).FirstOrDefault();
if (tryGetFileDbInfo != null)
{
if(tryGetFileDbInfo.Size == file.Length)
{
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
{
tryGetFileDbInfo = new MyDB.Class.FileInfoTable();
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}");*/
*//*Console.WriteLine($"Renew {file} --> {destinationFile}");*//*
copyList.Add((file.Length, file, destinationFile, "Renew"));
/*file.CopyTo(destinationFile, true);*/
*//*file.CopyTo(destinationFile, true);*//*
}
else
{
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length / 1024 / 1024}MB");
/*copyList.Add((file, destinationFile, 0, "Same"));*/
*//*copyList.Add((file, destinationFile, 0, "Same"));*//*
}
}
else
{
/*Console.WriteLine($"NewFile {file} --> {destinationFile}");*/
*//*Console.WriteLine($"NewFile {file} --> {destinationFile}");*//*
copyList.Add((file.Length, file, destinationFile, "NewFile"));
}
}*/
}
fileInfoDb.SaveChanges();
}
//遍历目录
foreach (var subDir in sourceDir.GetDirectories())
{
Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name));
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
/*Console.WriteLine($"Copy Error {source}");*/
/*Console.WriteLine(ex.ToString());*/
}
@ -70,8 +100,12 @@ public class FileCopy
public static long DicFileNum(string source)
{
long num = 0;
FileNum(source,ref num);
Console.WriteLine($"Totle Dic Num is {num}");
using (var fileInfoDb = new FileInfoDb())
{
num = fileInfoDb.Files.Where(w=>w.NeedChange==true).Count();
/*FileNum(source,ref num);*/
Debug.WriteLine($"Totle Dic Num is {num}");
}
return num;
}
private static void FileNum(string source, ref long num)
@ -88,60 +122,48 @@ public class FileCopy
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
/*Console.WriteLine($"Copy Error {source}");*/
/*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
{
var sourceDir = new DirectoryInfo(source);
/*try
{*/
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)
{
var desInfo = new FileInfo(Path.Join(des, file.Path.Replace(baseDic, "")));
var fileInfo = new FileInfo(file.Path);
if (!fileInfo.Exists)
{
Debug.WriteLine($"暂存位置文件消失 {fileInfo.FullName}");
continue;
}
//遍历目录
foreach (var subDir in sourceDir.GetDirectories())
if (!desInfo.Directory.Exists)
{
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);
if (desFileInfo.Length != file.Length)
{
Debug.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
{
Debug.WriteLine($"NewFile {file} --> {destinationFile}");
/*copyList.Add((file.Length, file, destinationFile, "NewFile"));*/
file.CopyTo(destinationFile, true);
desInfo.Directory.Create();
}
fileInfo.CopyTo(desInfo.FullName,true);
fileInfo.Delete();
file.NeedChange = false;
Debug.WriteLine($"moved the file and marked {fileInfo.FullName} --> {desInfo.FullName}");
doneNum++;
}
fileInfoDb.SaveChanges();
}
/*}
catch (Exception ex)
{
/*Console.WriteLine($"Copy Error {source}");*/
/*Console.WriteLine(ex.ToString());*/
}
Debug.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,12 +1,16 @@
using CopyUSB;
using CopyUSB.MyDB;
using System.Diagnostics;
using System.Management;
try
{
static string GetVolumeLabel(string driveName)
{
try
{
DriveInfo driveInfo = new DriveInfo(driveName);
return driveInfo.VolumeLabel;
return driveInfo.VolumeLabel.Length == 0 ? "default" : driveInfo.VolumeLabel;
}
catch (Exception)
{
@ -18,7 +22,7 @@ string query = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2";
ManagementEventWatcher managementEventWatcher = new(query);
Console.WriteLine("Waiting for USB...,take usb: DataCp");
Debug.WriteLine("Waiting for USB...,take usb: ZyData");
@ -28,12 +32,12 @@ managementEventWatcher.EventArrived += (s, e) =>
string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
Console.WriteLine(driveName);
Debug.WriteLine(driveName);
var dataDir = new DirectoryInfo(Path.Join("D:", "老师文档"));
if (GetVolumeLabel(driveName) == "DataCp")
if (GetVolumeLabel(driveName) == "ZyData")
{
Console.WriteLine("检测到拷出USB");
Debug.WriteLine("检测到拷出USB");
if (!dataDir.Exists)
{
dataDir.Create();
@ -41,13 +45,13 @@ managementEventWatcher.EventArrived += (s, e) =>
long doneNum = 0;
var copyTask = Task.Run(() =>
{
FileCopy.SimpleCopy(Path.Join("D:", "老师文档"), Path.Join(driveName, "FILE", "USBCopy"), ref doneNum);
FileCopy.SimpleCopy(Path.Join(driveName, "FILE", "USBCopy"), Path.Join("D:", "老师文档"), ref doneNum);
});
var planPrint = Task.Run(() =>
{
Console.WriteLine("scanning and get totle num");
Debug.WriteLine("scanning and get totle num");
var fileNumber = FileCopy.DicFileNum(Path.Join("D:", "老师文档"));
Console.WriteLine("scanned ok and get totle num");
Debug.WriteLine("scanned ok and get totle num");
while (!copyTask.IsCompleted)
{
@ -85,7 +89,7 @@ managementEventWatcher.EventArrived += (s, e) =>
});
Task.WaitAll(copyTask, planPrint);
Console.WriteLine("-----Copy out OK---------");
Debug.WriteLine("-----Copy out OK---------");
return;
}
@ -93,36 +97,19 @@ managementEventWatcher.EventArrived += (s, e) =>
string dPath = Path.Join("D:", "老师文档", GetVolumeLabel(driveName));
Console.WriteLine($"Detected: {driveName} || Save To -> : {dPath}");
Debug.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);
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)
{
/*Console.WriteLine($"Copy Error {copyItem.Item2}");*/
/*Console.WriteLine(ex.ToString());*/
}
}
Console.WriteLine("\n--------------Scaned and waiting--------------\n");
Debug.WriteLine("\n--------------Scaned and waiting--------------\n");
};
managementEventWatcher.Start();
@ -133,3 +120,7 @@ while (true)
Thread.Sleep(1000);
/*Console.WriteLine("一次休眠周期");*/
}
}catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}