u盘编程技巧

       在这个在研究u编程过程当中发现,window系统中本身就已经存在这样的API去处理这个usb编程方法.该方法如下

       protected override void WndProc(ref Message m)

       通过重载这个函数,可以通过消息触发的方式来获取U盘的文件信息.

       其他消息种类
       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;

        具体u盘处理类如下

        class DeviceDeal
    {
        private IList<string> diskList = new List<string>();

        delegate void SetTextCallback(string s);
        delegate void ClearListBoxItem();
        private ListBox _listbox;
        private Form _form;
        private Thread thread = null;

        //private BackgroundWorker backgroundworker;
        public DeviceDeal()
        {
            System.Timers.Timer timer = new System.Timers.Timer(1000);
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(fi);
            timer.AutoReset = true;
        }
        public void filldata(Form form, Message m,ListBox listbox)
        {
            _listbox = listbox;
            _form = form;
            try
            {
                //WM_DEVICECHANGE,系统硬件改变发出的系统消息
                if (m.Msg == WndProMsgConst.WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case WndProMsgConst.WM_DEVICECHANGE:
                            break;
                        //DBT_DEVICEARRIVAL,设备检测结束,并且可以使用
                        case WndProMsgConst.DBT_DEVICEARRIVAL:
                            scanUDisk();
                            _listbox.Items.Clear();
                            foreach (string s in diskList)
                            {
                                _listbox.Items.Add(s);
                            }
                            break;
                        //DBT_DEVICEREMOVECOMPLETE,设备卸载或者拔出
                        case WndProMsgConst.DBT_DEVICEREMOVECOMPLETE:
                            scanUDisk();
                            _listbox.Items.Clear();
                            foreach (string s in diskList)
                            {
                                _listbox.Items.Add(s);
                            }
                            break;

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("当前盘不能正确识别,请重新尝试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public void SetText(string text)
        {
            if (this._listbox.InvokeRequired)
            {
                if (_listbox.Items.Contains(text))
                    return;
                SetTextCallback d = new SetTextCallback(SetText);
                _form.Invoke(d, new object[] { text });
            }
            else
            {
                if (_listbox.Items.Contains(text))
                    return;
                this._listbox.Items.Add(text);
            }
        }
        void fi(object sender, System.Timers.ElapsedEventArgs e)
        {
            scanUDisk();
            //listBox1.Items.Clear();
            foreach (string s in diskList)
            {
                SetText(s);
            }
        }
       
        /// <summary>
        /// 扫描U口设备
        /// </summary>
        /// <param name="obj"></param>
        ///
        private void scanUDisk()
        {
            diskList.Clear();
            DriveInfo[] drives = DriveInfo.GetDrives();

            foreach (DriveInfo drive in drives)
            {
                if ((drive.DriveType == DriveType.Removable) && !drive.Name.Substring(0, 1).Equals("A"))
                {
                    try
                    {
                        diskList.Add(drive.Name);
                    }
                    catch
                    {
                        MessageBox.Show("当前盘不能正确识别,请重新尝试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值