C # Reading files from a USB flash drive

Source: Internet
Author: User

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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.