演示用递归的方法复制指定文件夹下所有文件(包括子文件夹)到指定位置,比较简单,主要是递归调用,以及字节流读取写入字节操作。直接上代码!
界面设计(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,off); 128 } while (off > 0); 129 readStream.Close(); 130 writStream.Flush(); 131 writStream.Close(); 132 } 133 134 } 135 }
//
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。