栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#中使用FilleStream实现视频文件的复制功能

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#中使用FilleStream实现视频文件的复制功能

实现

新建控制台程序

在main方法中

//复制源路径
string source = @"D:音乐迷音乐迷mv消愁-毛不易.mp4";
//复制目的路径
string target = @"C:UsersAdministratorDesktop霸道的程序猿.mp4";
CopyFile(source,target);
Console.WriteLine("复制成功");
Console.ReadKey();

然后实现复制视频文件的方法

public static void CopyFile(string source,string target)
    {
      //创建一个负责读取的流
      using (FileStream fsRead = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read))

      {
 //创建一个负责写入的流
 using (FileStream fdWrite = new FileStream(target,FileMode.OpenOrCreate,FileAccess.Write))
 {
   // 5 兆的字节数组
   byte[] buffer = new byte[1024*1024*5];
   //因为文件可能会比较大,所以我们在读取的时候 应该通过一个循环去读取
   while (true)
   {
     //返回本次读取实际读取到的字节数
     int r = fsRead.Read(buffer, 0, buffer.Length);
     //如果返回一个0 也就意味着什么都没有读取到 即读取完了
     if (r == 0)
     {
break;
     }
     fdWrite.Write(buffer,0,r);
   }

   
 }
      }
    }

运行效果

知识点扩展:C#使用FileStream复制多媒体文件,具体代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace 多媒体文件复制
{
  class Program
  {
    static void Main(string[] args)
    {
      //先读取出来,再写入到指定路径
      string source = @"C:123123.avi";
      string target = @"C:123456.avi";
      CopyFile(source, target);
    }
    public static void CopyFile(string source, string target)
    {
      //创建一个读取的流
      using (FileStream fsRead = new FileStream(source, FileMode.OpenOrCreate, FileAccess.Read))
      {
 //创建一个写入的流
 using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
 {
   //每次读取5M大小
   byte[] buffer = new byte[1024 * 1024 * 5];
   //文件可能比较大,循环去读
   while (true)
   {
     //本次实际读取到的字节数
     int r = fsRead.Read(buffer, 0, buffer.Length);
     //如果读取到的字节数为0,则意味着读完了
     if (r == 0)
     {
break;
     }
     fsWrite.Write(buffer, 0, r);
   }
 }
      }
    }
  }
}

总结

以上所述是小编给大家介绍的C#中使用FilleStream实现视频文件的复制功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122383.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号