c#大文件的拷贝

using System.IO;

namespace 数据流
{
    class Demo2
    {
        private string _strSourcePath = @"D:\httpd-2.4.37-o102q-x64-vc14-r2.zip";               //源文件路径
        private string _strTargetPath = @"E:\httpd-2.4.37-o102q-x64-vc14-r2.zip";               //目标文件路径
public void Test1()
        {
            if (File.Exists(_strSourcePath))
            {
                //读取文件流
                using (FileStream fsRead = new FileStream(_strSourcePath, FileMode.Open))
                { 
                    //写文件流
                    using(FileStream fsWrite=new FileStream(_strTargetPath,FileMode.OpenOrCreate))
                    {
                        byte[] byArrarRead = new byte[1024 * 1024];

                        while (true)
                        {
                            int rsCount = fsRead.Read(byArrarRead, 0, byArrarRead.Length);
                            fsWrite.Write(byArrarRead, 0, rsCount);

                            if (rsCount < byArrarRead.Length)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("路径不存在");
            }
        }

整体的思路:先获取到源文件和目标文件,我们选择流文件进行转移,就需要先读取数据流和写入数据流操作,然后分配内存地址,然后对二进制流进行真正的读写操作,直到全部读出为止。

 

有人不让我喝鸡汤

 

posted @ 2019-02-27 20:53  彩色的梦  阅读(370)  评论(0编辑  收藏  举报