• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c++异常处理(2)发布时间:2022-07-13
下一篇:
C/S架构的渗透测试-请求加解密及测试发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap