diff --git a/CopyUSB.csproj b/CopyUSB.csproj new file mode 100644 index 0000000..9d12198 --- /dev/null +++ b/CopyUSB.csproj @@ -0,0 +1,14 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + diff --git a/CopyUSB.sln b/CopyUSB.sln new file mode 100644 index 0000000..300c9f5 --- /dev/null +++ b/CopyUSB.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34031.279 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CopyUSB", "CopyUSB.csproj", "{04628109-4C1D-47E7-A573-9BAC1EE8B158}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {04628109-4C1D-47E7-A573-9BAC1EE8B158}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {04628109-4C1D-47E7-A573-9BAC1EE8B158}.Debug|Any CPU.Build.0 = Debug|Any CPU + {04628109-4C1D-47E7-A573-9BAC1EE8B158}.Release|Any CPU.ActiveCfg = Release|Any CPU + {04628109-4C1D-47E7-A573-9BAC1EE8B158}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FBB419A7-2122-486D-8ABC-D0A41B4BBBE0} + EndGlobalSection +EndGlobal diff --git a/FileCopy.cs b/FileCopy.cs new file mode 100644 index 0000000..e67bc38 --- /dev/null +++ b/FileCopy.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CopyUSB; + +public class FileCopy +{ + public static void Copy(string source,string des) + { + try + { + var sourceDir = new DirectoryInfo(source); + var desDir = new DirectoryInfo(des); + + // 确保目标目录存在 + if (!desDir.Exists) + { + desDir.Create(); + } + //遍历目录 + foreach (var subDir in sourceDir.GetDirectories()) + { + Copy(subDir.FullName, Path.Combine(desDir.FullName, subDir.Name)); + } + foreach (var file in sourceDir.GetFiles()) + { + string destinationFile = Path.Combine(desDir.FullName, file.Name); + file.CopyTo(destinationFile, true); + + Console.WriteLine($"{file} CopyTo--> {destinationFile}"); + } + } + catch (Exception ex) + { + Console.WriteLine($"{source} Copy Error"); + /*Console.WriteLine(ex.ToString());*/ + } + } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..231acb0 --- /dev/null +++ b/Program.cs @@ -0,0 +1,47 @@ +using CopyUSB; +using System.Management; +static string GetVolumeLabel(string driveName) +{ + try + { + DriveInfo driveInfo = new DriveInfo(driveName); + return driveInfo.VolumeLabel; + } + catch (Exception) + { + return null; + } +} + +string query = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2"; + +ManagementEventWatcher managementEventWatcher = new(query); + +Console.WriteLine("Waiting for USB..."); + + + +managementEventWatcher.EventArrived += (s, e) => +{ + string driveName = e.NewEvent.Properties["DriveName"].Value.ToString(); + + string dPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "USBCopy", GetVolumeLabel(driveName)); + + Console.WriteLine($"Detected: {driveName} || Save To -> : {dPath}"); + + if (!Directory.Exists(dPath)) + { + Directory.CreateDirectory(dPath); + } + FileCopy.Copy(driveName, dPath); + Console.WriteLine("Scaned and waiting ----------------------"); +}; + +managementEventWatcher.Start(); + +// 让程序保持运行状态 +while (true) +{ + Thread.Sleep(1000); + /*Console.WriteLine("一次休眠周期");*/ +} \ No newline at end of file