自动copyUSB不排序策略,使用多线程在copyUSB中根目录显示进度
This commit is contained in:
parent
ca29d275a8
commit
cd7c3385fa
82
FileCopy.cs
82
FileCopy.cs
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -46,7 +47,7 @@ public class FileCopy
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Same {file} --> {destinationFile} {file.Length/1024/1024}MB");
|
||||
Debug.WriteLine($"Same {file} --> {destinationFile} {file.Length/1024/1024}MB");
|
||||
/*copyList.Add((file, destinationFile, 0, "Same"));*/
|
||||
}
|
||||
}
|
||||
@ -62,7 +63,84 @@ public class FileCopy
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Copy Error {source}");
|
||||
/*Console.WriteLine($"Copy Error {source}");*/
|
||||
/*Console.WriteLine(ex.ToString());*/
|
||||
}
|
||||
}
|
||||
public static long DicFileNum(string source)
|
||||
{
|
||||
long num = 0;
|
||||
FileNum(source,ref num);
|
||||
Console.WriteLine($"Totle Dic Num is {num}");
|
||||
return num;
|
||||
}
|
||||
private static void FileNum(string source, ref long num)
|
||||
{
|
||||
try
|
||||
{
|
||||
var sourceDir = new DirectoryInfo(source);
|
||||
num += sourceDir.GetFiles().Length;
|
||||
//遍历目录
|
||||
foreach (var subDir in sourceDir.GetDirectories())
|
||||
{
|
||||
FileNum(subDir.FullName,ref num);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
/*Console.WriteLine($"Copy Error {source}");*/
|
||||
/*Console.WriteLine(ex.ToString());*/
|
||||
}
|
||||
}
|
||||
public static void SimpleCopy(string source, string des,ref Int64 doneNum)
|
||||
{
|
||||
try
|
||||
{
|
||||
var sourceDir = new DirectoryInfo(source);
|
||||
var desDir = new DirectoryInfo(des);
|
||||
|
||||
// 确保目标目录存在
|
||||
if (!desDir.Exists)
|
||||
{
|
||||
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 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);
|
||||
}
|
||||
doneNum++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
/*Console.WriteLine($"Copy Error {source}");*/
|
||||
/*Console.WriteLine(ex.ToString());*/
|
||||
}
|
||||
}
|
||||
|
98
Program.cs
98
Program.cs
@ -1,4 +1,5 @@
|
||||
using CopyUSB;
|
||||
using System.Diagnostics;
|
||||
using System.Management;
|
||||
static string GetVolumeLabel(string driveName)
|
||||
{
|
||||
@ -17,7 +18,9 @@ string query = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2";
|
||||
|
||||
ManagementEventWatcher managementEventWatcher = new(query);
|
||||
|
||||
Console.WriteLine("Waiting for USB...");
|
||||
Console.WriteLine("Waiting for USB...,take usb: DataCp");
|
||||
|
||||
|
||||
|
||||
managementEventWatcher.EventArrived += (s, e) =>
|
||||
{
|
||||
@ -26,8 +29,67 @@ managementEventWatcher.EventArrived += (s, e) =>
|
||||
|
||||
|
||||
Console.WriteLine(driveName);
|
||||
var dataDir = new DirectoryInfo(Path.Join("D:", "老师文档"));
|
||||
|
||||
if (GetVolumeLabel(driveName) == "DataCp")
|
||||
{
|
||||
Console.WriteLine("检测到拷出USB");
|
||||
if (!dataDir.Exists)
|
||||
{
|
||||
dataDir.Create();
|
||||
}
|
||||
long doneNum = 0;
|
||||
var copyTask = Task.Run(() =>
|
||||
{
|
||||
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");
|
||||
|
||||
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());
|
||||
|
||||
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));
|
||||
|
||||
@ -40,32 +102,8 @@ managementEventWatcher.EventArrived += (s, e) =>
|
||||
|
||||
var copyList = new List<(long, FileInfo, string, string)>();
|
||||
var sortedList=new List<(long, FileInfo, string, string)>();
|
||||
var dataDir = new DirectoryInfo(Path.Join("D:", "老师文档"));
|
||||
if (GetVolumeLabel(driveName) == "ZZIYU")
|
||||
{
|
||||
Console.WriteLine("检测到拷出USB");
|
||||
if (!dataDir.Exists)
|
||||
{
|
||||
dataDir.Create();
|
||||
}
|
||||
FileCopy.Copy(Path.Join("D:","老师文档"), Path.Join(driveName,"FILE", "USBCopy"), copyList);
|
||||
sortedList = copyList.OrderBy(s => s.Item1).Select(s => s).ToList();
|
||||
foreach (var copyItem in sortedList)
|
||||
{
|
||||
Console.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("Copy out OK---------");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
FileCopy.Copy(driveName, dPath,copyList);
|
||||
|
||||
@ -73,14 +111,14 @@ managementEventWatcher.EventArrived += (s, e) =>
|
||||
/*Console.WriteLine($"元素个数{sortedList.Count()}");*/
|
||||
foreach (var copyItem in sortedList)
|
||||
{
|
||||
Console.WriteLine($"{copyItem.Item4} {copyItem.Item2} --> {copyItem.Item3} {copyItem.Item1 / 1024 / 1024}MB");
|
||||
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($"Copy Error {copyItem.Item2}");*/
|
||||
/*Console.WriteLine(ex.ToString());*/
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user