做一个复制文件的程序,功能是当指定的文件夹下有文件时就拷贝到另一个文件夹下,并且当原来的文件夹下有新文件时也拷贝到第二个文件夹里面.请各位大侠帮忙,谢谢!

解决方案 »

  1.   

     System.IO.Directory.GetFiles
                
                System.IO.File.Copy
      

  2.   

    做一个windows服务。。按照你的操作写代码就可以了
      

  3.   

    这样?        private void StartWatch(string souPath, string desPath)
            {
                FileSystemWatcher watcher = new FileSystemWatcher(souPath);
                watcher.Created += delegate(object sender, FileSystemEventArgs e)
                {
                    if (e.ChangeType == WatcherChangeTypes.Created)
                    {
                        watcher.WaitForChanged(WatcherChangeTypes.Created);
                        File.Copy(e.FullPath, desPath + Path.GetFileName(e.FullPath), true);
                    }
                };
            }
      

  4.   

    好像那个if (e.ChangeType == WatcherChangeTypes.Created)
    和WaitForChanged是多于的
      

  5.   

    System.IO.Directory.GetFiles 
    System.IO.File.Copy
    怎么用啊?
    做一个windows服务,怎么做?
    只要有新的文件就拷贝到第二个文件夹里面。
    文件夹下的文件随时都会更新,怎么拷贝
      

  6.   

    一楼的就好了,楼主应该查msdn
      

  7.   

    大量的文件不也是用File.Copy,只是你要线程不阻塞的话就放到子线程去呗
      

  8.   

    当有很多新文件同时建立时,“StartWatch”这个程序还是能一个一个的把文件拷贝过来,是吗?
      

  9.   

    public static void CopyFile(string source_Path, string taget_Path)
            {
                try
                {
                    // 检查目标目录是否以目录分割字符结束如果不是则添加之
                    if (taget_Path[taget_Path.Length - 1] != Path.DirectorySeparatorChar)
                        taget_Path += Path.DirectorySeparatorChar;                 // 判断目标目录是否存在
                    if(!Directory.Exists(taget_Path)) Directory.CreateDirectory(taget_Path);
                     // 得到源目录的文件列表
                    string[] fileList = Directory.GetFileSystemEntries(source_Path);
                    // 遍历所有的文件和目录
                    foreach (string file in fileList)
                    {
                        // 若是文件夹 则Copy里面的文件
                        if(Directory.Exists(file))
                            CopyFile(file, taget_Path + Path.GetFileName(file));
                        // 否则直接Copy文件
                        else
                            File.Copy(file, taget_Path + Path.GetFileName(file), true);
                        
                    }
                }
                catch (Exception ex)
                {                MessageBox.Show(ex.ToString());            }
    }
      

  10.   

    private void timer1_Tick(object sender, EventArgs e)
            {
                DirectoryInfo d1 = new DirectoryInfo(pathold);
                DirectoryInfo d2 = new DirectoryInfo(pathnew);
                FileInfo[] file1 = d1.GetFiles();
                FileInfo[] file2 = d2.GetFiles();
                
                foreach (FileInfo finf in file1)
                {
                    if (finf.Extension.Equals(".txt"))  //如果扩展名为“.txt”
                    {
                        if (File.Exists(pathnew + @"\" + Path.GetFileName(finf.FullName)))
                        {
                            if (finf.LastWriteTime != File.GetLastWriteTime((pathnew + @"\" + Path.GetFileName(finf.FullName))))
                            {
                                finf.CopyTo(pathnew + @"\" + Path.GetFileName(finf.FullName), true);
                                
                            }
                            
                        }
                        else
                        {
                            finf.CopyTo(pathnew + @"\" + Path.GetFileName(finf.FullName), true);
                            
                        }
                    }
                }
            }
    这些代码暂时能实现复制文件的功能。