Fork me on Gitee

C#递归拷贝文件夹下文件以及文件夹

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

界面设计(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);此方法即可复制指定文件到指定目录。

 

posted @ 2019-08-06 20:54  VueDi  阅读(918)  评论(0编辑  收藏  举报