演示用递归的方法复制指定文件夹下所有文件(包括子文件夹)到指定位置,比较简单,主要是递归调用,以及字节流读取写入字节操作。直接上代码!

界面设计(WPF设计)

 1 <Window x:Class="LDDECryption.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:LDDECryption"
 7         mc:Ignorable="d"
 8         Title="MainWindow" Height="200" Width="600">
 9     <Grid>
10         <Grid.RowDefinitions>
11             <RowDefinition/>
12             <RowDefinition/>
13             <RowDefinition Height="auto"/>
14         </Grid.RowDefinitions>
15         <DockPanel Grid.Row="0" VerticalAlignment="Center">
16             <TextBlock Text="源路径"/>
17             <Button Content="..." Name="btnSourceUrl" DockPanel.Dock="Right" Width="60" Click="BtnSourceUrl_Click"/>
18             <TextBox Name="txtSourceUrl"/>
19         </DockPanel>
20         <DockPanel Grid.Row="1" VerticalAlignment="Center">
21             <TextBlock Text="目的路径"/>
22             <Button Content="..." Name="btnDesUrl" DockPanel.Dock="Right" Width="60" Click="BtnDesUrl_Click"/>
23             <TextBox Name="txtDesUrl"/>
24         </DockPanel>
25         <DockPanel Grid.Row="3">
26             <Button Content="确定" Name="btnOk" Width="60" Click="BtnOk_Click"/>
27         </DockPanel>
28     </Grid>
29 </Window>

后台代码

  1 using Microsoft.Win32;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.IO;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 using System.Windows;
  9 using System.Windows.Controls;
 10 using System.Windows.Data;
 11 using System.Windows.Documents;
 12 using System.Windows.Forms;
 13 using System.Windows.Input;
 14 using System.Windows.Media;
 15 using System.Windows.Media.Imaging;
 16 using System.Windows.Navigation;
 17 using System.Windows.Shapes;
 18
 19 namespace LDDECryption
 20 {
 21     /// <summary>
 22     /// MainWindow.xaml 的交互逻辑
 23     /// </summary>
 24     public partial class MainWindow : Window
 25     {
 26         public MainWindow()
 27         {
 28             InitializeComponent();
 29         }
 30
 31         private void BtnSourceUrl_Click(object sender, RoutedEventArgs e)
 32         {
 33             //OpenFileDialog openFileDialog = new OpenFileDialog();
 34             //openFileDialog.ShowDialog();
 35             FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
 36             folderBrowser.Description = "选择源路径";
 37             folderBrowser.ShowNewFolderButton = false;
 38             folderBrowser.RootFolder = Environment.SpecialFolder.MyComputer;
 39             if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 40             {
 41                 this.txtSourceUrl.Text = folderBrowser.SelectedPath;
 42             }
 43         }
 44
 45         private void BtnDesUrl_Click(object sender, RoutedEventArgs e)
 46         {
 47             FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
 48             folderBrowser.Description = "选择目的路径";
 49             folderBrowser.ShowNewFolderButton = true;
 50             folderBrowser.RootFolder = Environment.SpecialFolder.MyComputer;
 51             if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
 52             {
 53                 this.txtDesUrl.Text = folderBrowser.SelectedPath;
 54             }
 55         }
 56
 57         private void BtnOk_Click(object sender, RoutedEventArgs e)
 58         {
 59             var sour = this.txtSourceUrl.Text.Trim();
 60             if (!Directory.Exists(sour))
 61             {
 62                 System.Windows.MessageBox.Show("源路径不存在");
 63                 return;
 64             }
 65             var des = txtDesUrl.Text.Trim();
 66             if (!Directory.Exists(des))
 67             {
 68                 Directory.CreateDirectory(des);
 69             }
 70             DescryptionCopy(sour, des);
 71             System.Windows.MessageBox.Show("OK!", "", MessageBoxButton.OK);
 72         }
 73
 74         /// <summary>
 75         /// 复制文件夹==递归方法
 76         /// </summary>
 77         /// <param name="strSource">源文件夹</param>
 78         /// <param name="strDes">目标文件夹</param>
 79         private void DescryptionCopy(string strSource, string strDes)
 80         {
 81             if (!Directory.Exists(strSource))
 82             {
 83                 return;
 84             }
 85             //
 86             var strDesNew = System.IO.Path.Combine(strDes, System.IO.Path.GetFileName(strSource));
 87             if (!Directory.Exists(strDesNew))
 88             {
 89                 Directory.CreateDirectory(strDesNew);
 90             }
 91             var fileList = Directory.GetFileSystemEntries(strSource);
 92             if (fileList == null || fileList.Length == 0)
 93             {
 94                 return;
 95             }
 96             for (int i = 0; i < fileList.Length; i++)
 97             {
 98                 var file = fileList[i];
 99                 if (Directory.Exists(file))
100                 {
101                     var des = System.IO.Path.Combine(strDesNew, System.IO.Path.GetFileName(file));
102                     DescryptionCopy(file, strDesNew);
103                 }
104                 else
105                 {
106                     var des = System.IO.Path.Combine(strDesNew, System.IO.Path.GetFileName(file));
107                     FileCopy(file, des);
108                 }
109             }
110         }
111
112         /// <summary>
113         /// 文件拷贝
114         /// </summary>
115         /// <param name="fileSource"></param>
116         /// <param name="fileDestination"></param>
117         private void FileCopy(string fileSource, string fileDestination)
118         {
119             FileStream readStream = new FileStream(fileSource, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
120             var buff = new Byte[1024 * 1024 * 10];//10M
121             FileStream writStream = new FileStream(fileDestination, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
122             //
123             int off;
124             do
125             {
126                 off = readStream.Read(buff, 0, buff.Length);
127                 writStream.Write(buff, 0, off);
128             } while (off > 0);
129             readStream.Close();
130             writStream.Flush();
131             writStream.Close();
132         }
133
134     }
135 }

//

System.IO.File.Copy(file, path, true);此方法即可复制指定文件到指定目录。

转载于:https://www.cnblogs.com/DiKingVue/p/11311783.html

C#递归拷贝文件夹下文件以及文件夹相关推荐

  1. Java io流---拷贝文件夹下的所有文件和目录

    Java io流-拷贝文件夹下的所有文件和目录 代码: package demo01;import java.io.*; import java.util.TreeMap;public class C ...

  2. android删除文件夹代码,Android_Android递归方式删除某文件夹下的所有文件(.mp3文件等等),1.由于需要删除文件,因此需 - phpStudy...

    Android递归方式删除某文件夹下的所有文件(.mp3文件等等) 1.由于需要删除文件,因此需要如下权限: 2.核心代码 package com.example.deleteyoumi; impor ...

  3. java 文件 递归_JAVA实现遍历文件夹下的所有文件(递归调用和非递归调用)

    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...

  4. python 目录下的文件_用python把文件夹下的所有文件包括文件夹里面的文件都拷贝到同一个目录下...

    比如1文件夹下有2文件夹,2文件夹下有1.txt文件和3文件夹,3文件夹下有2.txt3.txt现在要把1.txt2.txt3.txt全都拷贝到1文件夹下importosimportshutildef ...

  5. C#中拷贝指定文件夹下的所有文件夹目录到指定文件夹中的方法

    原文地址:http://www.biye5u.com/article/Csharp/fileprog/2011/4198.html 本文给出了一个在C#中拷贝指定文件夹下的所有文件夹目录到指定文件夹中 ...

  6. Python递归获取指定文件夹下的所有文件夹、文件

    原文地址 分类目录--万能的Python系列 因为有了一个想从一个大文件夹下find出所有的.doc文件的需求,这个需求的关键活动就是递归获得文件夹下的所有文件.通过一番找资料,整理出两种递归获取指定 ...

  7. python查找文件夹中的指定文件_python 递归搜索文件夹下的指定文件

    python 递归搜索文件夹下的指定文件 import os def look_in_directory(directory): """Loop through the ...

  8. php递归遍历出文件夹下的所有文件和删除文件夹下的所有文件

    php递归删除目录下的所有文件: <?php header("content-type:text/html;charset=utf-8"); /** *删除指定目录()删除子 ...

  9. java题-如何递归遍历一个文件夹下的所有文件

    今天去面试了,笔试的时候遇到这个题印象深刻(因为不会),在此做出这个笔记,这个笔记是用了 http://blog.csdn.net/qq_27603235/article/details/507528 ...

  10. scp 保留文件属组_scp 对拷文件夹 和 文件夹下的所有文件 对拷文件并重命名

    对拷文件夹 (包括文件夹本身) scp -r   /home/wwwroot/www/charts/util root@192.168.1.65:/home/wwwroot/limesurvey_ba ...

最新文章

  1. 【SpringMVC 之应用篇】 1_SpringMVC入门 —— 第一个 Spring MVC 程序
  2. 向上造型和向下造型_盆景造型大全——造型教程
  3. spring mvc学习(44):springMVC运行原理
  4. 薅羊毛 Colab使用外部数据的7种方法!
  5. docker nacos mysql nginx 集群多台
  6. POJ 3122 Pie 二分枚举
  7. VC++常见错误原因解析之error LNK2019: 无法解析的外部符号 public: void __thiscall
  8. latex sign_LATEX科研论文写作教程
  9. python怎么弄成黑色背景图片_怎么能把图片的黑色背景改成透明背景
  10. 微软Exchange Server 2010 SP1下载
  11. POST 请求的三种常见数据提交格式
  12. Windows 10 安装 Maven
  13. exsi rh2288hv5 驱动_华为2288H V5阵列卡驱动下载|
  14. matlab中probIdx = 2 2,利用1stOpt1.5 pro来进行多元非线性拟合
  15. LaTeX中的参考文献-BibTeX
  16. $ is not defined
  17. Java mail outlook发邮件提示升级TLS1.2
  18. 《A Novel Approach to 3-D Gaze Tracking Using Stereo Cameras》论文阅读
  19. 推荐算法工程师面试准备
  20. ERR_ABORTED 404

热门文章

  1. indesign里怎么打根号_三相电是如何产生的?怎么接线?
  2. html页面的ajax请求,【提问】ajax请求返回整个html页面
  3. guido发布python版本的年份_Guido van Rossum
  4. 目标检测(九)--YOLO v1,v2,v3
  5. python字典键值可以是元组吗_python – 为同一个字典值创建可交换元组键...
  6. python指定时间执行程序_如何在特定时间执行程序
  7. Windows10下鼠标跳屏问题——Microsoft Serial Ballpoint
  8. Android开发环境搭建(Android Studio安装)
  9. php中如何判断目录是否存在文件_PHP判断指定目录下是否存在文件
  10. MySQL join 与where的执行顺序