添加项目文件。
This commit is contained in:
parent
d69ba9a6ab
commit
731aef1d88
14
CopyUSB.csproj
Normal file
14
CopyUSB.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Management" Version="7.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
25
CopyUSB.sln
Normal file
25
CopyUSB.sln
Normal file
@ -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
|
42
FileCopy.cs
Normal file
42
FileCopy.cs
Normal file
@ -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());*/
|
||||
}
|
||||
}
|
||||
}
|
47
Program.cs
Normal file
47
Program.cs
Normal file
@ -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("一次休眠周期");*/
|
||||
}
|
Loading…
Reference in New Issue
Block a user