当把u盘放插入,然后程序自动将u盘的内容复制到本地硬盘
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盘更新
{
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盘插入
driveinfo[] s = driveinfo.getdrives();
foreach (driveinfo drive in s)
{
if (drive.drivetype == drivetype.removable)
{
listbox1.items.add("u盘已插入,盘符为:" + 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盘卸载
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);
}
在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
最大的网站源码资源下载站,
新闻热点
疑难解答