C # obtain the USB event API

Source: Internet
Author: User

Const int WM_DEVICECHANGE = 0x2190;
Constint DBT_DEVICEARRIVAL = 0x8000;
Const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
Protected override void WndProc (ref Message m)
{
Try
{
// If (m. Msg = WM_DEVICECHANGE)
//{
Switch (m. WParam. ToInt32 ())
{
Case DBT_DEVICEARRIVAL: // USB flash drive
DriveInfo [] s = DriveInfo. GetDrives ();
Foreach (DriveInfo drive in s)
{
If (drive. DriveType = DriveType. Removable)
{
Console. WriteLine ("USB insertion ");
Break;
}
}
Break;
Case DBT_DEVICEREMOVECOMPLETE: // unmount the USB flash drive
//
Console. WriteLine ("USB unmount ");
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.