方法:
/// <summary> /// 文件复制 /// </summary> /// <param name="source">源文件,例如:@"D:media复制.avi"</param> /// <param name="target">目标文件,例如:@"D:media复制.avi"</param> public void FileStreamUseCopy(string source,string target) { //思路:就是先将需要复制的文件读取出来,然后再写入到制定的位置 //创建一个读取的流 using(FileStream fsRead=new FileStream(source,FileMode.OpenOrCreate,FileAccess.Read)) { //创建一个写入的流 using(FileStream fsWrite=new FileStream(target,FileMode.OpenOrCreate,FileAccess.Write)) { byte[] buffer = new byte[1024 * 1024 * 2]; int r=0; //r 本次实际读取到的字节数 while((r=fsRead.Read(buffer,0,buffer.Length))>0) { fsWrite.Write(buffer, 0, r); } } } }
调用测试:
static void Main(string[] args) { Program p = new Program(); //p.FileStreamUseWrite(); //写入 //p.FileStreamUseRead(); //读取 string source = @"D:20170220_StudyMyDonemedia8、使用FileStream实现多媒体文件的复制.avi"; string target = @"D:20170220_StudyMyDonemedia ew.avi"; p.FileStreamUseCopy(source, target); Console.WriteLine("复制成功"); Console.ReadLine(); }