监控PenDrive操作

时间:2014-05-08 13:56:44

标签: c# usb

网。我开发了一个C#项目来监控Pen-drive动作。 通过使用wndproc方法,我发现usb插件并通过设备更改事件发生插件操作。但我不知道如何找到从笔式驱动器复制到PC的所有文件,反之亦然,并且还会阻止用户在插入pendrive后的特定时间访问笔式驱动器。请提供解决方案或任何想法。提前致谢。对不起,我的英语不好。我在

下面提供了设备更改事件的示例代码
protected override void WndProc(ref Message m)
        {
            try
            {
                var drvs = DriveInfo.GetDrives().Where(drives => drives.IsReady && drives.DriveType == DriveType.Removable);

                //0x0219 device change.
                //0x8000 device arrival.
                //0x8004 device removal complete.
                //0x00000002 device type volume.
                /*if (m.Msg == 0x0219)
                {
                    ejectUSB();
                    /*if (m.Msg == 0x8004)
                    {
                        Thread thread = new Thread(ejectUSB);
                        thread.SetApartmentState(ApartmentState.STA);
                        thread.Start();
                        writeLog("Enters for USB Ejection ");
                    }*
                }*/
                switch (m.Msg)
                {
                    case WM_DEVICECHANGE: OnDeviceChange(ref m); break;
                }
            }
            catch (Exception e)
            {
                writeLog(e.ToString());
            }
            base.WndProc(ref m);
        }
        void OnDeviceChange(ref Message msg)
        {
            int wParam = (int)msg.WParam;
            if (wParam == DBT_DEVICEARRIVAL)
            {
                writeLog("Enters for USB Ejection ");
                ejectUSB();
                //MessageBox.Show("Device Arrived");
            }
            else if (wParam ==  DBT_DEVICEREMOVECOMPLETE)
            {
                writeLog("USB is Removed ");
                //MessageBox.Show("Device Removed");
            } 
        }

0 个答案:

没有答案