Form1.cs
上传用户:lyzyl198
上传日期:2008-05-19
资源大小:174k
文件大小:4k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. namespace USB
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public const int WM_DEVICECHANGE = 0x219;
  14.         public const int DBT_DEVICEARRIVAL = 0x8000;
  15.         public const int DBT_CONFIGCHANGECANCELED = 0x0019;
  16.         public const int DBT_CONFIGCHANGED = 0x0018;
  17.         public const int DBT_CUSTOMEVENT = 0x8006;
  18.         public const int DBT_DEVICEQUERYREMOVE = 0x8001;
  19.         public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
  20.         public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
  21.         public const int DBT_DEVICEREMOVEPENDING = 0x8003;
  22.         public const int DBT_DEVICETYPESPECIFIC = 0x8005;
  23.         public const int DBT_DEVNODES_CHANGED = 0x0007;
  24.         public const int DBT_QUERYCHANGECONFIG = 0x0017;
  25.         public const int DBT_USERDEFINED = 0xFFFF;
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.         }
  30.         private void Form1_Load(object sender, EventArgs e)
  31.         {
  32.         }
  33.         protected override void WndProc(ref Message m)
  34.         {
  35.             try
  36.             {
  37.                 if (m.Msg == WM_DEVICECHANGE)
  38.                 {
  39.                     switch (m.WParam.ToInt32())
  40.                     {
  41.                         case WM_DEVICECHANGE://
  42.                             break;
  43.                         case DBT_DEVICEARRIVAL://U盘插入
  44.                             DriveInfo[] s = DriveInfo.GetDrives();
  45.                             foreach (DriveInfo drive in s)
  46.                             {
  47.                                 if (drive.DriveType == DriveType.Removable)
  48.                                 {
  49.                                     richTextBox1.AppendText("U盘已插入,盘符为:" + drive.Name.ToString() + "rn");
  50.                                     break;
  51.                                 }
  52.                             }
  53.                             break;
  54.                         case DBT_CONFIGCHANGECANCELED:
  55.                             MessageBox.Show("2");
  56.                             break;
  57.                         case DBT_CONFIGCHANGED:
  58.                             MessageBox.Show("3");
  59.                             break;
  60.                         case DBT_CUSTOMEVENT:
  61.                             MessageBox.Show("4");
  62.                             break;
  63.                         case DBT_DEVICEQUERYREMOVE:
  64.                             MessageBox.Show("5");
  65.                             break;
  66.                         case DBT_DEVICEQUERYREMOVEFAILED:
  67.                             MessageBox.Show("6");
  68.                             break;
  69.                         case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
  70.                             richTextBox1.AppendText("U盘已卸载,盘符为:");
  71.                             break;
  72.                         case DBT_DEVICEREMOVEPENDING:
  73.                             MessageBox.Show("7");
  74.                             break;
  75.                         case DBT_DEVICETYPESPECIFIC:
  76.                             MessageBox.Show("8");
  77.                             break;
  78.                         case DBT_DEVNODES_CHANGED://可用,设备变化时
  79.                             MessageBox.Show("9");
  80.                             break;
  81.                         case DBT_QUERYCHANGECONFIG:
  82.                             MessageBox.Show("10");
  83.                             break;
  84.                         case DBT_USERDEFINED:
  85.                             MessageBox.Show("11");
  86.                             break;
  87.                         default:
  88.                             break;
  89.                     }
  90.                 }
  91.             }
  92.             catch (Exception ex)
  93.             {
  94.                 MessageBox.Show(ex.Message);
  95.             }
  96.             base.WndProc(ref m);
  97.         }
  98.         private void button1_Click(object sender, EventArgs e)
  99.         {
  100.            
  101.         }
  102.     }
  103. }