解决方案 »

  1.   

    fileSystemWatcher.Filter = "*.xl*",这样会把所有的Excel文件包括模板都会监控到,如果我只想监控xls,xlsx怎么办?
      

  2.   

    fileSystemWatcher.Filter = "*.xls"
      

  3.   

    你可以监控所有文件,当监测到文件发生变化时候在判断static void watcher_Changed(object sender, FileSystemEventArgs e)
            {
                string extend = e.FullPath.Substring(e.FullPath.LastIndexOf('.'));
                switch (extend)
                {
                    case ".xls":
                        break;
                    case ".doc":
                        break;
                    default:
                        break;
                }
            }
      

  4.   

    谢谢你的给的方法,另外我想在这个过程里面启动Timer怎么启动?
    我在这个事件里加了Timer.Enable=true和Timer.Start()都不能启动,要怎么传参数才可以?
      

  5.   


    在实例化Timer后,要绑定事件处理timer1.Elapsed


    要注意的是,启动时,要等一个interval的时间才会触发,并且可以重入(在一个周期内,事件未处理完,仍然可以进入第二个周期处理)
      

  6.   

    http://msdn.microsoft.com/zh-cn/library/system.timers.timer.elapsed(v=VS.80).aspx
      

  7.   

    private void t2_Elapsed(object source, ElapsedEventArgs e)
            {
                if (this.running == false)
                {
                    string readSql = "select * from [Folder]";
                    fswCmd = new OleDbCommand(readSql, fswCon);
                    readRead = fswCmd.ExecuteReader();
                    while (readRead.Read())
                    {
                        this.upList2.Add((string)readRead["change"]);
                        this.upList2.Add((string)readRead["dataName"]);
                        this.upList2.Add((string)readRead["fileName"]);
                        this.upList2.Add((string)readRead["fileType"]);
                        this.upList2.Add((string)readRead["fullPath"]);
                    }
                    t2.Enabled = false;
                    t2.Stop();
                    this.Invoke((MethodInvoker)delegate { this.Updatelist(); });
                    readCmd.Dispose();
                    if (upList2.Count != 0 && this.backgroundWorker2.IsBusy != true) { this.backgroundWorker2.RunWorkerAsync(); }
                }
            }老大呀,我这段里面查询字段简直就是乱跳呀,查询字段里面一会出错一会没错一会加第一个一会加第四个
      

  8.   

    t2.AutoReset = false;这个是引起乱跳的关键,怎么反复循环呢?自己判断条件停止
      

  9.   

    目前不敢让Timer根据条件判断自己停止了,希望双11玩回来的大大们帮帮我解决
      

  10.   

    启动Timer?为什么要启动定时器呢?做什么用呢?我没有理解你的意思。
    但是要告诉你的是FileSystemWatcher这个类不需要启动定时器去轮循监控文件改动,
    只需要设置EnableRaisingEvents = true;启用控件则开始监听了。
    当监听到文件改动后会自动调用Created(创建文件)或者Changed(文件修改或目录更改)或Deleted(删除)事件,你要做的是在对应事件里面写你的逻辑就好啦。
      

  11.   

    你好,因为我刚学这个没多久,所以方法比较笨:
    一个Timer、一个FileSystemWatcher、一个backgroundWorker
    1.FileSystemWatcher监控发生变化写入一个临时Access文件记录哪些变化,同时启动Timer
    2.Timer启动后到了设定时间,把这个临时Access读出并写入数组后对数组循环查找这个临时Acceess并删除记录,之后启动backgroundWorker,当Timer再次到设定时间时,如果临时Access没有记录就希望它停止
    3.backgroundWorker启动后根据不同路径记录到不同的Access中
    过程可能比大侠们繁杂,我这个菜鸟只能用这种笨办法来解决问题,现在是这个Timer我不知道怎么启动,所以只能让它不停地对临时Access文件访问
      

  12.   

    另外因为我不知道在FileSystemWatche发生变化时直接写入到数据库会不会有问题?比如说同时有大批的文件创建或删除,或者有大文件发生变化时,这个写入到数据库的过程来不来得及,会不会有什么问题?