//USB设备感应模块(应用底层较为代码)
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x219)
{
if (m.WParam.ToInt32() == 0x8000)
{
DriveInfo[] allDrivers = DriveInfo.GetDrives();
foreach (var d in allDrivers) // 系统会自动扫描包含C:\\ 之类的,以及我们插入的U盘和移动硬盘
{
if (allDrivers[i].IsReady)
if (d.IsReady)
{
if (allDrivers[i].DriveType == DriveType.Removable) //查找到的是移动式的 磁盘
{
//(在这里填写你需处理的代码)
DialogResult dr = MessageBox.Show("是否要拷贝U盘中的信息?", "U盘", MessageBoxButtons.OKCancel);if (dr == DialogResult.OK)
{
MessageBox.Show("正在搜索U盘中数据,请稍后~");
}
}
}
}
}
}
base.WndProc(ref m);
方法二:使用C#方法 (使用一个定时器)
private void timer1_Tick(object sender, EventArgs e)
{
DriveInfo[] allDrivers = DriveInfo.GetDrives();
foreach (var d in allDrivers)
{
if (d.IsReady)
{
if (d.DriveType == DriveType.Removable)
{
string[] filenames = Directory.GetFileSystemEntries(d.Name);
foreach (string file in filenames) //依次访问该磁盘中的文件或文件夹
{
if (Path.GetFileName(file) == "office") //判断该移动磁盘中是否有某个文件夹或文件
{
this.timer1.Enabled = false //暂时关闭定时器
DialogResult dr = MessageBox.Show("是否要拷贝U盘中的信息?", "U盘", MessageBoxButtons.OKCancel);
if (dr == DialogResult.OK)
{
//MessageBox.Show("正在搜索U盘中数据,请稍后~");
this.label1.Text = "磁盘位置:" + d.Name;
}
break;
}
}
}
}
}
}
this.timer1.Enabled = true; //开启定时器
}