增加了数据库优化策略

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))
foreach (var file in sourceDir.GetFiles())
{
var desFileInfo = new FileInfo(destinationFile);
if (desFileInfo.Length != file.Length)
string destinationFile = Path.Combine(desDir.FullName, file.Name);
var tryGetFileDbInfo = fileInfoDb.Files.Where(w => w.Path == destinationFile).FirstOrDefault();
if (tryGetFileDbInfo != null)
{
/*Console.WriteLine($"Renew {file} --> {destinationFile}");*/
copyList.Add((file.Length,file, destinationFile,"Renew"));
/*file.CopyTo(destinationFile, true);*/
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
{
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length/1024/1024}MB");
/*copyList.Add((file, destinationFile, 0, "Same"));*/
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}");*//*
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
{
/*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();
}
//遍历目录
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 allNeedChangeFile = fileInfoDb.Files.Where(w => w.NeedChange == true).OrderBy(o => o.Size).ToList();
foreach (var file in allNeedChangeFile)
{
var desFileInfo = new FileInfo(destinationFile);
if (desFileInfo.Length != file.Length)
var desInfo = new FileInfo(Path.Join(des, file.Path.Replace(baseDic, "")));
var fileInfo = new FileInfo(file.Path);
if (!fileInfo.Exists)
{
Debug.WriteLine($"Renew {file} --> {destinationFile}");
/*copyList.Add((file.Length, file, destinationFile, "Renew"));*/
file.CopyTo(destinationFile, true);
Debug.WriteLine($"暂存位置文件消失 {fileInfo.FullName}");
continue;
}
else
if (!desInfo.Directory.Exists)
{
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length / 1024 / 1024}MB");
/*copyList.Add((file, destinationFile, 0, "Same"));*/
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++;
}
else
{
Debug.WriteLine($"NewFile {file} --> {destinationFile}");
/*copyList.Add((file.Length, file, destinationFile, "NewFile"));*/
file.CopyTo(destinationFile, true);
}
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,59 +1,79 @@
using CopyUSB;
using CopyUSB.MyDB;
using System.Diagnostics;
using System.Management;
static string GetVolumeLabel(string driveName)
try
{
try
static string GetVolumeLabel(string driveName)
{
DriveInfo driveInfo = new DriveInfo(driveName);
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)
try
{
dataDir.Create();
DriveInfo driveInfo = new DriveInfo(driveName);
return driveInfo.VolumeLabel.Length == 0 ? "default" : driveInfo.VolumeLabel;
}
long doneNum = 0;
var copyTask = Task.Run(() =>
catch (Exception)
{
FileCopy.SimpleCopy(Path.Join("D:", "老师文档"), Path.Join(driveName, "FILE", "USBCopy"), ref doneNum);
});
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");
return "default";
}
}
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);
if (usbRoot.GetFiles().Where(w => w.Name == $"{(int)((float)doneNum / (float)fileNumber * 100)}.upd").ToArray().Length!=1)
dataDir.Create();
}
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();
Debug.WriteLine(doneNum.ToString());
@ -61,75 +81,46 @@ managementEventWatcher.EventArrived += (s, e) =>
{
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"));
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());
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);
Task.WaitAll(copyTask, planPrint);
Debug.WriteLine("-----Copy out OK---------");
return;
}
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}");*/
/*Console.WriteLine(ex.ToString());*/
Directory.CreateDirectory(dPath);
}
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");
};
managementEventWatcher.Start();
// 让程序保持运行状态
while (true)
}catch(Exception ex)
{
Thread.Sleep(1000);
/*Console.WriteLine("一次休眠周期");*/
Debug.WriteLine(ex.ToString());
}