您当前的位置: 首页 >  c#

彭世瑜

暂无认证

  • 1浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C#编程-124:复制多媒体文件

彭世瑜 发布时间:2017-08-16 22:10:58 ,浏览量:1

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace MoveFileTest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             //思路:先将要复制的多媒体文件读取出来,然后在写入到指定位置
  13.             string source = @"C:\Users\pengshiyu\Desktop\source\微信图片_20170709221953.jpg";
  14.             string target = @"C:\Users\pengshiyu\Desktop\source\微信图片.jpg";
  15.             CopyFile(source,target);
  16.             Console.WriteLine("多媒体文件复制成功");
  17.             Console.ReadKey();
  18.         }
  19.         public static void CopyFile(string source, string target)
  20.         { 
  21.         //1、创建一个负责读取的流
  22.             using (FileStream fsRead = new FileStream(source, FileMode.Open, FileAccess.Read))
  23.             { 
  24.              
  25.             //2、创建一个写入流
  26.                 using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
  27.                 { 
  28.                 //3、多媒体文件较大,循环读取
  29.                     byte[] buffer = new byte[1024 * 1024 * 5];//5M
  30.                     while (true)
  31.                     { 
  32.                     //返回本次读取实际读取到的字节数
  33.                         int r = fsRead.Read(buffer, 0, buffer.Length);
  34.                         //如果返回0,什么也没读取到
  35.                         if (r == 0) break;
  36.                         fsWrite.Write(buffer,0,r);//最后一次读取,可能不是5M
  37.                          
  38.                     }
  39.  
  40.                 }
  41.             }
  42.              
  43.             }
  44.     }
  45. }
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.0963s