c# 新手 ,请问怎么找出一个文件夹中内容变动过的文件,复制到另一个文件夹

解决方案 »

  1.   

    参考http://www.cnblogs.com/springyangwc/archive/2011/08/27/2155547.html
      

  2.   

    FileInfo.CreationTime  文件的创建时间
    FileInfo.LastWriteTime 文件的上次写操作时间 
    两者不同,可视为被改动了
      

  3.   

    也可以先过去文件md5,发现变动后用新md5再次验证。
      

  4.   


    你好,这样写对不对?      private void CopyDifFile(string srcPath, string aimPath)
            {
                DirectoryInfo directory = new DirectoryInfo(srcPath);
                var files = directory.GetFiles()
                 .Where(file => file.LastWriteTime != file.CreationTime);
                string s = "";
              
                foreach (FileInfo file in files)
                {
                    s = file.Name;
                    if (s != "")
                    {
                        MessageBox.Show("文件已存在");
                        break;
                        //CopyDir(srcPath + s, aimPath + Path.GetFileName(s));
                    }
                    // 否则直接Copy文件
                    else
                    {
                        MessageBox.Show("文件" + s + "更改");
                        System.IO.File.Copy(srcPath + Path.GetFileName(s), aimPath + Path.GetFileName(s), true);
                    }
                }
            }
      

  5.   

    监测文件变化可以使用FileSystemWatcher
    使用方法参考这个或者自行搜索
    https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
    因为涉及IO操作,所以相关文件夹的权限设置是少不了的
      

  6.   

    想找出srcpath 中所有变动的文件 复制到 aimpath 中,上面代码有错误,不知道怎么改,求帮助
      

  7.   

    这样用
                var p = new FileInfo("1.txt");
                Console.WriteLine(p.CreationTime);
                Console.WriteLine(p.LastWriteTime);
      

  8.   

           private void CopyDifFile(string srcPath, string aimPath)
            {
                DirectoryInfo directory = new DirectoryInfo(srcPath);
               var files = directory.GetFiles()
                 .Where(file => file.LastWriteTime != file.CreationTime);
                //FileInfo file = new FileInfo(srcPath);
                string s = "";
                //fi.CopyTo(aimPath, true);
                foreach (FileInfo file in files)
                {
                    s = file.Name;
                    if (s != "")
                    {
                        MessageBox.Show("文件已存在");
                        break;
                        //file.CopyTo(aimPath,true);
                        //CopyDir(srcPath + s, aimPath + Path.GetFileName(s));
                    }
                    // 否则直接Copy文件
                    else
                    {
                        MessageBox.Show("文件" + s + "更改");
                        System.IO.File.Copy(srcPath + Path.GetFileName(s), aimPath + Path.GetFileName(s), true);
                    }
                }
            }
      

  9.   

    这个可以用FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path
    watcher.Changed += Watcher_Changed;
    private void Watcher_Changed(object sender, FileSystemEventArgs e)
            {
               //这里写操作方法不就行了吗
            }FileSystemWatcher watcher = new FileSystemWatcher();
      

  10.   

    好像文件的增删查改都可以的,然后在把这个程序做成window services,就省心了FileSystemWatcher curWatcher = new FileSystemWatcher();
                curWatcher.BeginInit();
                curWatcher.IncludeSubdirectories = true;
                //curWatcher.Path = ConfigurationManager.AppSettings["FileMonitorDirectory"];
                curWatcher.Path = "D:\\images";            curWatcher.Changed += new FileSystemEventHandler(OnFileChanged);            curWatcher.Created += new FileSystemEventHandler(OnFileCreated);            curWatcher.Deleted += new FileSystemEventHandler(OnFileDeleted);            curWatcher.Renamed += new RenamedEventHandler(OnFileRenamed);            curWatcher.EnableRaisingEvents = true;            curWatcher.EndInit();
      

  11.   

    使用文件监视就可以了:FileSystemWatcher