C # 's USB communication programming

Source: Internet
Author: User
Tags foreach continue
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



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.