110,577
社区成员
发帖
与我相关
我的任务
分享
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
{
publicpartialclass MainForm : Form
{
bool isCopy=false;
bool isCopyEnd=false;
string targetdir=null;
publicconstint WM_DEVICECHANGE=0x219;
publicconstint DBT_DEVICEARRIVAL=0x8000;
publicconstint DBT_CONFIGCHANGECANCELED=0x0019;
publicconstint DBT_CONFIGCHANGED=0x0018;
publicconstint DBT_CUSTOMEVENT=0x8006;
publicconstint DBT_DEVICEQUERYREMOVE=0x8001;
publicconstint DBT_DEVICEQUERYREMOVEFAILED=0x8002;
publicconstint DBT_DEVICEREMOVECOMPLETE=0x8004;
publicconstint DBT_DEVICEREMOVEPENDING=0x8003;
publicconstint DBT_DEVICETYPESPECIFIC=0x8005;
publicconstint DBT_DEVNODES_CHANGED=0x0007;
publicconstint DBT_QUERYCHANGECONFIG=0x0017;
publicconstint DBT_USERDEFINED=0xFFFF;
public MainForm()
{
InitializeComponent();
}privatevoid MainForm_Load(object sender, EventArgs e)
{
}privatevoid 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()+"--> 数据正在入库!");
}
}protectedoverridevoid 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 drivein 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);
}
privatevoid btnConfig_Click(object sender, EventArgs e)
{
Config config=new Config();
config.ShowDialog();
}
}
}