首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

检测U盘插入并自动复制U盘里文件到D盘 C#源码,该如何处理

2012-05-15 
检测U盘插入并自动复制U盘里文件到D盘 C#源码各位高手帮帮这个怎样做,检测U盘插入并自动复制U盘里文件到D

检测U盘插入并自动复制U盘里文件到D盘 C#源码
各位高手帮帮这个怎样做,检测U盘插入并自动复制U盘里文件到D盘的源码
1.功能检测U盘
2.把U盘的文件COPY到硬盘
大体就是这样的

[解决办法]

C# code
using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using MTClient.Utility;
using System.Threading;

namespace UDiskMonitor
{
  public partial class MainForm : Form
  {
    bool isCopy = false;
    bool isCopyEnd = false;
    string targetdir = null;

    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;

    public MainForm()
    {
      InitializeComponent();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {

    }

    private void CopyFile(string path)
    {
      isCopyEnd = true;
      if (isCopy)
      {
        DirectoryHelper dir = new DirectoryHelper();
        targetdir = DateTime.Now.ToString();
        targetdir = targetdir.Replace(':', '-');
        targetdir = ConfigReader.GetValue("targetdir") + targetdir;
        //ConfigurationManager.AppSettings["targetdir"].ToString() + targetdir;
        if (!Directory.Exists(targetdir))
        {
          Directory.CreateDirectory(targetdir);
        }
        else
        {
          listBox1.Items.Add(DateTime.Now.ToString() + "--> 文件夹已经存在,请确认!");
          return;
        }

        dir.CopyDirectoryAndFiles(targetdir, new DirectoryInfo(path));
        listBox1.Items.Add(DateTime.Now.ToString() + "--> 已完成数据拷贝!");
        listBox1.Items.Add(DateTime.Now.ToString() + "--> 正在检查文件合法性!");
        listBox1.Items.Add(DateTime.Now.ToString() + "--> 文件合法!");
        listBox1.Items.Add(DateTime.Now.ToString() + "--> 数据正在入库!");
      }
    }

    protected override void WndProc(ref Message m)
    {
      try
      {
        if (m.Msg == WM_DEVICECHANGE)
        {
          switch (m.WParam.ToInt32())


          {
            case WM_DEVICECHANGE:
              break;
            case DBT_DEVICEARRIVAL://U盘插入
              DriveInfo[] s = DriveInfo.GetDrives();
              foreach (DriveInfo drive in s)
              {
                if (drive.DriveType == DriveType.Removable)
                {
                  listBox1.Items.Add(DateTime.Now.ToString() + "--> U盘已插入,盘符为:" + drive.Name.ToString());
                  Thread.Sleep(1000);
                  if (!isCopyEnd)
                  {
                    isCopy = true;
                    CopyFile(drive.Name + ConfigurationManager.AppSettings["sourcedir"].ToString());
                  }
                  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: //U盘卸载
              listBox1.Items.Add(DateTime.Now.ToString() + "--> U盘已卸载!");
              isCopy = false;
              isCopyEnd = false;
              break;
            case DBT_DEVICEREMOVEPENDING:
              break;
            case DBT_DEVICETYPESPECIFIC:
              break;
            case DBT_DEVNODES_CHANGED:
              break;
            case DBT_QUERYCHANGECONFIG:
              break;
            case DBT_USERDEFINED:
              break;
            default:
              break;
          }
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
      base.WndProc(ref m);
    }

    private void btnConfig_Click(object sender, EventArgs e)
    {
      Config config = new Config();


      config.ShowDialog();
    }
  }
}


这就是核心代码了。给你参考下。
留QQ 纪念 290072014

热点排行