更改try catch逻辑
This commit is contained in:
parent
3a3f866bf4
commit
c07715536c
136
Program.cs
136
Program.cs
@ -3,8 +3,6 @@ using CopyUSB.MyDB;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Management;
|
using System.Management;
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
static string GetVolumeLabel(string driveName)
|
static string GetVolumeLabel(string driveName)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -28,36 +26,54 @@ try
|
|||||||
|
|
||||||
managementEventWatcher.EventArrived += (s, e) =>
|
managementEventWatcher.EventArrived += (s, e) =>
|
||||||
{
|
{
|
||||||
/*Console.WriteLine("触发器被触发了");*/
|
try
|
||||||
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");
|
/*Console.WriteLine("触发器被触发了");*/
|
||||||
if (!dataDir.Exists)
|
string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
|
||||||
{
|
|
||||||
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)
|
|
||||||
|
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());
|
||||||
|
|
||||||
@ -65,51 +81,39 @@ try
|
|||||||
{
|
{
|
||||||
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);
|
|
||||||
Debug.WriteLine("-----Copy out OK---------");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
string dPath = Path.Join("D:", "老师文档", GetVolumeLabel(driveName));
|
string dPath = Path.Join("D:", "老师文档", GetVolumeLabel(driveName));
|
||||||
|
|
||||||
Debug.WriteLine($"Detected: {driveName} || Save To -> : {dPath}");
|
Debug.WriteLine($"Detected: {driveName} || Save To -> : {dPath}");
|
||||||
|
|
||||||
if (!Directory.Exists(dPath))
|
if (!Directory.Exists(dPath))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(dPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FileCopy.Copy(driveName, dPath);
|
||||||
|
|
||||||
|
/*Console.WriteLine($"元素个数{sortedList.Count()}");*/
|
||||||
|
Debug.WriteLine("\n--------------Scaned and waiting--------------\n");
|
||||||
|
}catch(Exception ex)
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(dPath);
|
Debug.WriteLine(ex.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
FileCopy.Copy(driveName, dPath);
|
|
||||||
|
|
||||||
/*Console.WriteLine($"元素个数{sortedList.Count()}");*/
|
|
||||||
Debug.WriteLine("\n--------------Scaned and waiting--------------\n");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
managementEventWatcher.Start();
|
managementEventWatcher.Start();
|
||||||
@ -120,7 +124,3 @@ try
|
|||||||
Thread.Sleep(1000);
|
Thread.Sleep(1000);
|
||||||
/*Console.WriteLine("一次休眠周期");*/
|
/*Console.WriteLine("一次休眠周期");*/
|
||||||
}
|
}
|
||||||
}catch(Exception ex)
|
|
||||||
{
|
|
||||||
Debug.WriteLine(ex.ToString());
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user