备忘-复制指定文件夹中的所有文件(包括子文件夹)到指定文件夹中的类

本文记录了如何使用Java编程实现将一个包含子文件夹的目录及其内容完整复制到另一个指定的目标目录下,涵盖了递归复制文件夹的逻辑和文件操作细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FilesCopy
{
   public class FileCopier
    {
       private string source;
       private string target;
       private string fileType;
       public void FileCopier(string source,string target,string fileType="")
       {
           this.source = source;
           this.target = target;
           this.fileType = fileType;
       }


       public void Copy(string source, string target, string fileType = "")
       {
           if (!Directory.Exists(source))
           {
               MessageBox.Show("源文件夹不存在");
               return;
           }
           if (!Directory.Exists(target))
           {
               MessageBox.Show("目标文件夹不存在");
               return;
           }
           List<FileItem> lst = new List<FileItem>();
           lock (lst)
           {
               GetFile(lst, new DirectoryInfo(source),target,fileType);
           }
           foreach (var f in lst)
           {
               ThreadPool.QueueUserWorkItem(new WaitCallback(CopyFile), f);
           }
       }


       void CopyFile(object model)
       {
           var item = model as FileItem;
           string targetFile = Path.Combine(item.Target, item.Name + "." + item.FileType);
           File.Copy(item.Path, targetFile);
       }

       void GetFile(List<FileItem> lst, DirectoryInfo sorce,string target,string fileType)
       {
           DirectoryInfo[] lstD = sorce.GetDirectories();
           if (lstD != null && lstD.Count() != 0)
           {
               foreach (var d in lstD)
               {
                   GetFile(lst, d,target,fileType);
               }
           }
           FileInfo[] lstF = sorce.GetFiles();
           if (lstF != null && lstF.Count() != 0)
           {
               for (int i = 0; i < lstF.Count(); i++)
               {
                   if (!string.IsNullOrEmpty(fileType))
                   {
                       if (lstF[i].Extension != fileType)
                       {
                           continue;
                       }
                   }
                   lst.Add(new FileItem()
                   {
                       Name = sorce.Name + i.ToString(),
                       Path = lstF[i].FullName,
                       FileType = lstF[i].Extension
                   });
               }
           }
       }


       class FileItem
       {
           public string Target { get; set; }
           public string Path { get; set; }
           public string FileType { get; set; }
           public string Name { get; set; }
       }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值