Programming
When the USB disk is inserted, the program automatically copies the contents of the U disk to the local hard drive
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 = 0xFFFF;
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://u Disk Insert
Driveinfo[] s = driveinfo.getdrives ();
foreach (DriveInfo drive in s)
{
if (drive. DriveType = = drivetype.removable)
{
LISTBOX1.ITEMS.ADD ("U disk has been inserted, the letter is:" + 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://u disk Uninstall
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);
}
The information I found at http://www.codeproject.com/useritems/usbeject.asp.
Volumedeviceclass Volumedeviceclass = new Volumedeviceclass ();
foreach (Volume device in volumedeviceclass.devices)
{
Is this volume on USB disks?
if (!device. ISUSB)
Continue
Is this volume a logical disk?
if (device. Logicaldrive = = null) | | (Device. logicaldrive.length = 0))
Continue
Device. Eject (TRUE); Allow Windows to display any relevant UI
}
Http://www.codeproject.com/useritems/usbeject.asp