www.cnblogs.com/freeliver54/archive/2008/10/13/1309720.html 

解决方案 »

  1.   

    COPY过来给你using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;namespace ImageManager
    {
        public partial class BackgroundWorkerForm : Form
        {
            public BackgroundWorkerForm()
            {
                InitializeComponent();
                //
                timer1.Interval = 10000;
                timer1.Enabled = true;
                CheckForIllegalCrossThreadCalls = false;
            }        private void button1_Click(object sender, EventArgs e)
            {
                int iFileCount = 0;
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"F:\Temp\A");
                iFileCount = di.GetFiles().Length;
                MessageBox.Show("A文件夹下 文件的个数" + iFileCount.ToString());
            }        bool IsComplete = true;
            //目的是后台定时运行文件同步操作 使A文件夹中的文件与B文件夹中的同步
            //先将A文件夹中存在而B文件夹中没有的A文件夹中的文件删除
            //再直接将B文件夹下的文件全部复制到A文件夹下
            //或者将B文件夹中存在A文件夹中没有或比A文件夹中的更新的B文件夹中的文件复制到A文件夹下 
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                IsComplete = false;//本次操作未完成            System.IO.DirectoryInfo diA = new System.IO.DirectoryInfo(@"F:\Temp\A");            
                System.IO.FileInfo[] fiA = diA.GetFiles();            
                //先将A文件夹中存在而B文件夹中没有的A文件夹中的文件删除
                foreach (FileInfo tmpfiA in fiA)
                {
                    if (!File.Exists(@"F:\Temp\B\" + tmpfiA.Name))
                    {
                        tmpfiA.Delete();
                        this.label2.Text += "\n A Delete "+tmpfiA.Name;
                    }
                }
                //再直接将B文件夹下的文件全部复制到A文件夹下
                System.IO.DirectoryInfo diB = new System.IO.DirectoryInfo(@"F:\Temp\B");
                System.IO.FileInfo[] fiB = diB.GetFiles();
                //foreach (FileInfo tmpfiB in fiB)
                //{
                //    tmpfiB.CopyTo(@"F:\Temp\A\" + tmpfiB.Name, true);
                //}
                //或者将B文件夹中存在A文件夹中没有或比A文件夹中的更新的B文件夹中的文件复制到A文件夹下 
                foreach (FileInfo tmpfiB in fiB)
                {
                    if (File.Exists(@"F:\Temp\A\" + tmpfiB.Name))
                    {
                        FileInfo tmp = new System.IO.FileInfo(@"F:\Temp\A\" + tmpfiB.Name);
                        if (tmpfiB.LastWriteTime.CompareTo(tmp.LastWriteTime) != 0)
                        {
                            tmpfiB.CopyTo(@"F:\Temp\A\" + tmpfiB.Name, true);
                            this.label2.Text += "\n A Update " + tmpfiB.Name;
                        }
                    }
                    else
                    {
                        tmpfiB.CopyTo(@"F:\Temp\A\" + tmpfiB.Name, true);
                        this.label2.Text += "\n A Copy " + tmpfiB.Name;
                    }
                }            IsComplete = true;//本次操作已完成 可进行下次操作
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                if (IsComplete)
                {
                    this.backgroundWorker1.RunWorkerAsync();
                }
                this.label1.Text = DateTime.Now.ToString();
            }        
        }
    }
      

  2.   

    整个工作线程,定时检查两个文件夹的内容。就是楼上的代码。
    另外,也可考虑用FileSystemWatcher来监控一个文件夹的变动,然后相应的修改另一个文件夹。
      

  3.   

    用FileSystemWathcer會觸發幾個事件,哪不是同一文件重復處理嗎
      

  4.   

    用filewatcher控件比较容易实现子文件也可以。
      

  5.   


    filewatcher要怎麼實現在
    如果有子目錄的話會觸發幾個事情呢