Chinaunix首页 | 论坛 | 博客
  • 博客访问: 827112
  • 博文数量: 190
  • 博客积分: 2991
  • 博客等级: 少校
  • 技术积分: 2400
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-24 18:11
文章分类

全部博文(190)

文章存档

2015年(3)

2014年(1)

2013年(65)

2012年(121)

我的朋友

分类: C/C++

2012-09-24 22:09:14

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dome
{
    class Program
    {
        static void Main(string[] args)
        {
            //大文件拷贝实例
            string source = @"E:\Develop Tools\VS2010.iso";

            string target = @"D:\VS2010.iso";

            CopyFile(source,target);
           
            Console.ReadKey();
        }

        private static void CopyFile(string source, string target)
        {
            //1,创建一个读取文件流
            using (FileStream fs = new FileStream(source,FileMode.Open))
            {
                //2,创建一个写入文件流
                using (FileStream fsWrite = new FileStream(target,FileMode.Create))
                {
                    //3,创建一个读取文件,写入文件的缓冲区
                    byte[] buffer = new byte[1024 * 1024 * 10];
                    //4,开始读写文件
                    while (true)
                    {
                        int r = fs.Read(buffer, 0, buffer.Length);

                        //文件读取完毕
                        if (r<=0)
                        {
                            break;
                        }
                        else
                        {
                            fsWrite.Write(buffer, 0, r);
                        }
                    }
                }
            }

        }
    }
}
阅读(1354) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~