When you insert a USB flash drive, the program automatically copies the content of the USB flash drive to the local hard disk.
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. IO;
Namespace U disk update
{
Public partial class Form1: Form
{
Public const int WM_DEVICECHANGE = 0x219;
Public const int DBT_DEVICEARRIVAL = 0x8000;
Public const int DBT_CONFIGCHANGECANCELED = 0x0019;
Public const int DBT_CONFIGCHANGED = 0x0018;
Public const int DBT_CUSTOMEVENT = 0x8006;
Public const int DBT_DEVICEQUERYREMOVE = 0x8001;
Public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
Public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
Public const int DBT_DEVICEREMOVEPENDING = 0x8003;
Public const int DBT_DEVICETYPESPECIFIC = 0x8005;
Public const int DBT_DEVNODES_CHANGED = 0x0007;
Public const int DBT_QUERYCHANGECONFIG = 0x0017;
Public const int DBT_USERDEFINED = 0 xFFFF;
Public Form1 ()
{
InitializeComponent ();
}
Private void Form1_Load (object sender, EventArgs e)
{
}
Protected override void WndProc (ref Message m)
{
Try
{
If (m. Msg = WM_DEVICECHANGE)
{
Switch (m. WParam. ToInt32 ())
{
Case WM_DEVICECHANGE:
Break;
Case DBT_DEVICEARRIVAL: // USB flash drive
DriveInfo [] s = DriveInfo. GetDrives ();
Foreach (DriveInfo drive in s)
{
If (drive. DriveType = DriveType. Removable)
{
ListBox1.Items. Add ("U disk inserted, drive letter:" + drive. Name. ToString ());
Break;
}
}
Break;
Case DBT_CONFIGCHANGECANCELED:
Break;
Case DBT_CONFIGCHANGED:
Break;
Case DBT_CUSTOMEVENT:
Break;
Case DBT_DEVICEQUERYREMOVE:
Break;
Case DBT_DEVICEQUERYREMOVEFAILED:
Break;
Case DBT_DEVICEREMOVECOMPLETE: // unmount the USB flash drive
Break;
Case DBT_DEVICEREMOVEPENDING:
Break;
Case DBT_DEVICETYPESPECIFIC:
Break;
Case DBT_DEVNODES_CHANGED:
Break;
Case DBT_QUERYCHANGECONFIG:
Break;
Case DBT_USERDEFINED:
Break;
Default:
Break;
}
}
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message );
}
Base. WndProc (ref m );
}