| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 526 人关注过本帖
标题:求助:USB盘文件夹监测,异常
只看楼主 加入收藏
sunflove
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-6-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
求助:USB盘文件夹监测,异常
本人写了个文件夹实时监测,原意用在读取一照相机U盘的更新的文件。测试时,拷贝到指定文件夹下是,异常,程序消失。
namespace USBMonitor
{
    public partial class Form1 : Form
    {

        public const int WM_DEVICECHANGE = 0x219;
        public const int DBT_DEVICEARRIVAL = 0x8000;
        public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;

        FileSystemWatcher watcher = new FileSystemWatcher();


        public Form1()
        {
            InitializeComponent();            
        }

        protected override void WndProc(ref Message m)
        {
            try
            {
                //当设备被插入/拔出的时候,WINDOWS会向每个窗体发送WM_DEVICECHANGE 消息,
                //当消息的wParam 值等于 DBT_DEVICEARRIVAL 时,表示Media设备被插入并且已经可用;
                //如果wParam值等于DBT_DEVICEREMOVECOMPLETE,表示Media设备已经被移出。

                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)
                            {
                                //Removable表示此驱动器是一个可移动存储设备,如软盘驱动器或 USB 闪存驱动器。
                                if (drive.DriveType == DriveType.Removable)
                                {
                                    //drive.RootDirectory  获取驱动器的根目录。
                                    listBox1.Items.Add("U盘已插入,盘符为:" + drive.Name.ToString());
                                    //DICM根据实际情况,也可在图形界面手动添加
                                    textBox1.Text = drive.Name.ToString() + "DICM";
                                    monitor();
                                    break;
                                }
                            }
                            break;

                       case DBT_DEVICEREMOVECOMPLETE: //U盘卸载
                            break;
                      default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.WndProc(ref m);
        }


        public void monitor()
        {
            watcher.Path = textBox1.Text;
            listBox1.Items.Add("路径:" + textBox1.Text);

            /* 设置为监视 LastWrite 和 LastAccess 时间方面的更改,以及目录中文本文件的创建、删除或重命名。 */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            // 只监控.txt文件
            watcher.Filter = "*.jpg";

            // 添加事件处理器。
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            //watcher.Renamed += new RenamedEventHandler(OnRenamed);  
            
            watcher.EnableRaisingEvents = true;
        }

        // 定义事件处理器。
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            //如果更改、创建或删除文件,文件路径将被输出到控制台。
            string path1 = e.FullPath;            
            string path2 = textBox2.Text + e.Name;
            //string path2 = "e:\\" + e.Name;
            File.Copy(path1, path2);
            listBox1.Items.Add("文件:" + e.FullPath + " is copyed.");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.ShowDialog();
            textBox2.Text = folderBrowserDialog1.SelectedPath.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //监测USB文件夹的变化,并复制至制定文件夹
        private void button3_Click(object sender, EventArgs e)
        {
            
        }
    }
}
搜索更多相关主题的帖子: 文件夹 照相机 public 
2011-06-30 10:27
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:20 
太深奥了

   唯实惟新 至诚致志
2011-06-30 10:38
快速回复:求助:USB盘文件夹监测,异常
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027720 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved