演示用遞歸的方法複製指定文件夾下所有文件(包括子文件夾)到指定位置,涉及位元組流讀取寫入位元組操作 ...
演示用遞歸的方法複製指定文件夾下所有文件(包括子文件夾)到指定位置,比較簡單,主要是遞歸調用,以及位元組流讀取寫入位元組操作。直接上代碼!
界面設計(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);此方法即可複製指定文件到指定目錄。