using System;
using System.IO;namespace FrameworkExamples
{
    //HOW TO: recursively copy all the files and sub dirs in a given directory
    //        to another directory    class SampleRecursiveCopy
    {
        static void Main()
        {
            string srcdir, destdir;
            bool   recursive;            recursive = true;
            srcdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images");
            destdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images2");            FileCopy(srcdir, destdir, recursive);
        }        private static void FileCopy(string srcdir, string destdir, bool recursive)
        {
            DirectoryInfo   dir;
            FileInfo[]      files;
            DirectoryInfo[] dirs;
            string          tmppath;            //determine if the destination directory exists, if not create it
            if (! Directory.Exists(destdir))
            {
                Directory.CreateDirectory(destdir);
            }            dir = new DirectoryInfo(srcdir);
            
            //if the source dir doesn't exist, throw
            if (! dir.Exists)
            {
                throw new ArgumentException("source dir doesn't exist -> " + srcdir);
            }            //get all files in the current dir
            files = dir.GetFiles();            //loop through each file
            foreach(FileInfo file in files)
            {
                //create the path to where this file should be in destdir
                tmppath=Path.Combine(destdir, file.Name);                                //copy file to dest dir
                file.CopyTo(tmppath, false);
            }            //cleanup
            files = null;
            
            //if not recursive, all work is done
            if (! recursive)
            {
                return;
            }            //otherwise, get dirs
            dirs = dir.GetDirectories();            //loop through each sub directory in the current dir
            foreach(DirectoryInfo subdir in dirs)
            {
                //create the path to the directory in destdir
                tmppath = Path.Combine(destdir, subdir.Name);                //recursively call this function over and over again
                //with each new dir.
                FileCopy(subdir.FullName, tmppath, recursive);
            }
            
            //cleanup
            dirs = null;
            
            dir = null;
        }
    }
}

解决方案 »

  1.   

    我的答案是解决你的这个实际问题.思路就是查找c:\tmp下的所有文件,发现在d:\tmp下有相同文件名称的就拷贝过了. 先写一个函数进行拷贝.private void MyCopy(string strDirFrom,string strDirTo)
    {
    // strDirFrom = @"d:\tmp";
    // strDirTo = @"c:\tmp";
    DirectoryInfo dirInfo = new DirectoryInfo(strDirTo);
    foreach(FileInfo infoFile in dirInfo.GetFiles())
    {

    if (File.Exists(strDirFrom + "\\" + infoFile.Name))
    File.Copy(strDirFrom + "\\" + infoFile.Name,strDirTo + "\\" + infoFile.Name,true);
    }
    }2.在窗体中添加一个Timer,设置Enable = True,Interval为合适的时间.在Tick时间中,调用上面的函数.
    MyCopy(@"d:\tmp",@"c:\tmp");
      

  2.   

    如果那位高手写有完整的程序(最好是可执行文件),请发给我好吗/谢谢了
     email: [email protected]
    qq;20534654
      

  3.   

    给可思路是不错,代码也很好,但自己拿来用时,问题很多,MY GOD!
      

  4.   

    yuansuibo108(波波)你真让我恶心