欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-29 14:18:29
演示用递归的方法复制指定文件夹下所有文件(包括子文件夹)到指定位置,涉及字节流读取写入字节操作 ......

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

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