1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 //添加命名空间 8 using System.IO; 9 10 namespace 文件流 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 string source = @"C:\Users\Administrator\Desktop\增加.gif"; 17 18 string target = @"C:\Users\Administrator\Desktop\new.gif"; 19 20 Command.CopyFile(source, target); 21 22 Console.WriteLine("复制成功!"); 23 24 Console.ReadKey(); 25 } 26 } 27 28 //操作类 29 public class Command 30 { 31 public static void CopyFile(string source, string target) 32 { 33 //创建读取流 34 using(FileStream fsRead = new FileStream(source, FileMode.Open, FileAccess.Read)) 35 { 36 //创建写入流 37 using(FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write)) 38 { 39 //字节数组,存放取出的字节数 40 byte[] buffer = new byte[1024 * 1024 * 5]; 41 42 //循环,当i!=0的时候循环 43 while (true) 44 { 45 //当前是假存到字节数组中的字节数 46 int i = fsRead.Read(buffer, 0, buffer.Length); 47 48 //如果i==0,break; 49 if (i == 0) 50 { 51 break; 52 } 53 54 //写入数据 55 fsWrite.Write(buffer, 0, i); 56 } 57 } 58 } 59 } 60 } 61 }