当前位置: 移动技术网 > IT编程>开发语言>c# > C#检测是否有u盘插入的方法

C#检测是否有u盘插入的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#检测是否有u盘插入的方法。分享给大家供大家参考。具体如下:

该c#代码可监控是否有u盘插入,同时可以监控其它驱动器的变化

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.runtime.interopservices
;
namespace windowsapplication16
{
 public partial class form1 : form
 {
  public form1()
  {
   initializecomponent();
  }
  [structlayout(layoutkind.sequential)]
  public struct dev_broadcast_volume
  {
   public int dbcv_size;
   public int dbcv_devicetype;
   public int dbcv_reserved;
   public int dbcv_unitmask;
  }
  protected override void wndproc(ref message m)
  {
   // 发生设备变动
   const int wm_devicechange = 0x0219;
   // 系统检测到一个新设备
   const int dbt_devicearrival = 0x8000;
   // 系统完成移除一个设备
   const int dbt_deviceremovecomplete = 0x8001;
   // 逻辑卷标
   const int dbt_devtyp_volume = 0x00000002;
   switch (m.msg)
   {
    case wm_devicechange:
     switch (m.wparam.toint32())
     {
      case dbt_devicearrival:
       int devtype = marshal.readint32(m.lparam, 4);
       if (devtype == dbt_devtyp_volume)
       {
        dev_broadcast_volume vol;
        vol = (dev_broadcast_volume)marshal.ptrtostructure(
         m.lparam, typeof(dev_broadcast_volume));
        messagebox.show(vol.dbcv_unitmask.tostring("x"));
       }
       break;
      case dbt_deviceremovecomplete:
       messagebox.show("removal");
       break;
     }
     break;
   }
   base.wndproc(ref m);
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网