首页 > 代码库 > C#控制台基础 filestream实现文件的复制粘贴

C#控制台基础 filestream实现文件的复制粘贴

1、代码

 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7  8 namespace ConsoleApplication3 9 {10     class Program11     {12         static void Main(string[] args)13         {14             //读取文件的路径15             string pathRead = @"O:\EasyBCD 2.2 Beta - Build 169.exe";16             17             //我要把文件存到哪个路径下,并改变文件的名字18             string pathSave = @"L:\EasyBCD.exe";19 20             using (FileStream fsRead = new FileStream(pathRead, FileMode.Open, FileAccess.Read))21             {22                 using (FileStream fsWrite = new FileStream(pathSave, FileMode.Create, FileAccess.Write))23                 {24                     //每次读取500KB25                     byte[] buffer = new byte[500 * 1024];26                     int r = fsRead.Read(buffer,0,buffer.Length);27                     double fileMov = 0;28                     while (r>0)29                     {30                         fsWrite.Write(buffer, 0, r);31                         32                         //存取此次转移的大小是多少KB33                         fileMov=fileMov+ r / 1024.0;34                         Console.WriteLine("转移了"+r/1024.0+"KB");35                         r = fsRead.Read(buffer, 0, buffer.Length);36                     }37                     Console.WriteLine("该文件大小"+fileMov+"KB");38                 }39             }40             Console.ReadKey();41         }42     }43 }

 

 

2、效果

技术分享

C#控制台基础 filestream实现文件的复制粘贴