zoukankan      html  css  js  c++  java
  • 利用递归处理文件夹拷贝功能

    递归:

    一个过程(或函数)直接或间接调用自己本身,这种过程(或函数)叫递归过程(或函数);通俗来讲就是一个函数可以调用函数本身。下面通过一个文件夹的拷贝功能来阐述

    • 需求

    实现一个简单的文件夹拷贝功能(文件夹下存在子目录文件夹),拷贝后的文件结构不变;

    • 界面

     源文件(左)结构如下:

     后台代码:

     1     public partial class Form1 : Form
     2     {
     3         public Form1()
     4         {
     5             InitializeComponent();
     6         }
     7 
     8         private void buttonEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
     9         {
    10             ButtonEdit bte = (ButtonEdit)sender;
    11 
    12             FolderBrowserDialog fbd = new FolderBrowserDialog();
    13 
    14             if (fbd.ShowDialog() == DialogResult.OK)
    15             {
    16                 if (bte == btnSource)
    17                     this.btnSource.Text = fbd.SelectedPath;
    18                 else
    19                     this.btnTarget.Text = fbd.SelectedPath;
    20                 target = btnTarget.Text;
    21                 source = btnSource.Text;
    22             }
    23         }
    24 
    25         /// <summary>
    26         /// 源文件夹路径
    27         /// </summary>
    28         private string source { set; get; }
    29 
    30         /// <summary>
    31         /// 目标文件夹路径
    32         /// </summary>
    33         private string target { set; get; }
    34 
    35         private void btnCopy_Click(object sender, EventArgs e)
    36         {
    37             MoveFolder(source, target);
    38             txtlog.AppendText("拷贝成功!");
    39         }
    40 
    41         private void MoveFolder(string sourceFolder, string destFolder)
    42         {
    43             //检查是否存在目标目录
    44             if (!Directory.Exists(destFolder))
    45             {
    46                 Directory.CreateDirectory(destFolder);
    47             }
    48 
    49             //第一步先获取文件夹
    50             DirectoryInfo info = new DirectoryInfo(sourceFolder);
    51 
    52             //获取文件下的所有文件
    53             FileInfo[] files = info.GetFiles();
    54             foreach (FileInfo file in files)
    55             {
    56                 file.CopyTo(destFolder + "\" + file.Name, true);
    57                 txtlog.AppendText("正在拷贝文件: " + file.FullName + "
    ");
    58             }
    59 
    60             //文件夹
    61             FileSystemInfo[] fsi = info.GetFileSystemInfos();
    62 
    63             foreach (FileSystemInfo item in fsi)
    64             {
    65                 if (string.IsNullOrEmpty(item.Extension))
    66                 {
    67                     //利用递归处理
    68                     MoveFolder(item.FullName, destFolder + GetFolderName(item.FullName));
    69                 }
    70             }
    71         }
    72 
    73         /// <summary>
    74         /// 获取源文件所在文件夹
    75         /// </summary>
    76         /// <param name="sourceFolder"></param>
    77         /// <returns></returns>
    78         private string GetFolderName(string sourceFolder)
    79         {
    80             string[] strs = sourceFolder.Split(new char[] { '\' });
    81             //获取源文件名
    82             return "\" + strs[strs.Count() - 1];
    83         }
    84     }

    目标文件夹结构:

  • 相关阅读:
    阐述:SIP协议是什么
    【SIP协议】学习初学笔记
    【协议学习】SIP基本场景分析
    电话的前世今生
    深入浅出SIP协议
    QVariant类及QVariant与自定义数据类型转换的方法
    Qt中如何根据类名来实例化对象
    模板的全特化与偏特化
    为什么c++中,有时可以用类名直接访问非静态成员函数?
    C++引用详解
  • 原文地址:https://www.cnblogs.com/tuqun/p/3727548.html
Copyright © 2011-2022 走看看