在.NET中探测U盘的插入/拔出

  有同学向我问这个问题,于是就Google了一下找到答案,不过是C下的,我将其改编成了C#的。

  当设备被插入/拔出的时候,WINDOWS会向每个窗体发送WM_DEVICECHANGE 消息,当消息的wParam 值等于 DBT_DEVICEARRIVAL 时,表示Media设备被插入并且已经可用;如果wParam值等于DBT_DEVICEREMOVECOMPLETE,表示Media设备已经被移出。

它们的lParam都指向一个 DEV_BROADCAST_HDR结构体,其原形如下:

1  typedef  struct  _DEV_BROADCAST_HDR
2  {
3      DWORD dbch_size;
4      DWORD dbch_devicetype;
5      DWORD dbch_reserved;
6  } DEV_BROADCAST_HDR,  * PDEV_BROADCAST_HDR;

这个结构体仅仅是一个“头”(HDR),其后还有附加数据,dbch_size表示结构体实例的字节数,当其中的dbch_devicetype字段值等于 DBT_DEVTYP_VOLUME时,表示当前设备是逻辑驱动器,且lParam实际上指向的应该是 DEV_BROADCAST_VOLUME 结构体实例(真佩服这种逻辑), DEV_BROADCAST_VOLUME 结构体原形如下:

1  typedef  struct  _DEV_BROADCAST_VOLUME {
2      DWORD dbcv_size;
3      DWORD dbcv_devicetype;
4      DWORD dbcv_reserved;
5      DWORD dbcv_unitmask;
6      WORD dbcv_flags;
7  } DEV_BROADCAST_VOLUME,  * PDEV_BROADCAST_VOLUME;
其中 dbcv_unitmask 字段表示当前改变的驱动器掩码,第一位表示驱动器号A,第二位表示驱动器号B,第三位表示驱动器号C,以此类推……  dbcv_flags 表示驱动器的类别,如果等于1,则是光盘驱动器;如果是2,则是网络驱动器;如果是硬盘、U盘则都等于0

  所以,我只需要在程序中捕捉 WM_DEVICECHANGE 消息,然后根据具体情况去处理即可,下面是我的测试代码:

None.gif using  System;
None.gif
using  System.Runtime.InteropServices;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  UDiskDetect
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class Form1 : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential)]
InBlock.gif        
struct DEV_BROADCAST_HDR
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public UInt32 dbch_size;
InBlock.gif            
public UInt32 dbch_devicetype;
InBlock.gif            
public UInt32 dbch_reserved;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential)]
InBlock.gif        
struct DEV_BROADCAST_VOLUME
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public UInt32 dbcv_size;
InBlock.gif            
public UInt32 dbcv_devicetype;
InBlock.gif            
public UInt32 dbcv_reserved;
InBlock.gif            
public UInt32 dbcv_unitmask;
InBlock.gif            
public UInt16 dbcv_flags;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void DefWndProc(ref Message m)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (m.Msg == 0x0219)//WM_DEVICECHANGE
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                
switch (m.WParam.ToInt32())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case 0x8000://DBT_DEVICEARRIVAL
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            DEV_BROADCAST_HDR dbhdr 
= (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
InBlock.gif
InBlock.gif                            
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif{
InBlock.gif                                DEV_BROADCAST_VOLUME dbv 
= (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
InBlock.gif                                
if (dbv.dbcv_flags == 0)
InBlock.gif                                    AddVolumes(GetVolumes(dbv.dbcv_unitmask));
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                    
case 0x8004://DBT_DEVICEREMOVECOMPLETE
ExpandedSubBlockStart.gifContractedSubBlock.gif
                        dot.gif{
InBlock.gif                            DEV_BROADCAST_HDR dbhdr 
= (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
InBlock.gif
InBlock.gif                            
if (dbhdr.dbch_devicetype == 0x00000002)//DBT_DEVTYP_VOLUME
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif{
InBlock.gif                                DEV_BROADCAST_VOLUME dbv 
= (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
InBlock.gif                                
if (dbv.dbcv_flags == 0)
InBlock.gif                                    RemoveVolumes(GetVolumes(dbv.dbcv_unitmask));
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.DefWndProc(ref m);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据驱动器掩码返回驱动器号数组
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Mask">掩码</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回驱动器号数组</returns>

InBlock.gif        public static char[] GetVolumes(UInt32 Mask)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            List
<char> Volumes = new List<char>();
InBlock.gif
InBlock.gif            
for (int i = 0; i < 32; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
uint p = (uint)Math.Pow(2, i);
InBlock.gif                
if ((p | Mask) == p)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Volumes.Add((
char)('A' + i));
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return Volumes.ToArray();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void AddVolumes(char[] Volumes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (char volume in Volumes)
InBlock.gif                listBox1.Items.Add(volume);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void RemoveVolumes(char[] Volumes)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (char volume in Volumes)
InBlock.gif                listBox1.Items.Remove(volume);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/AndyHai/archive/2007/07/25/830224.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值