大文件拷贝,可以显示进度和耗时

 

class Program
    {
        static void Main(string[] args)
        {
            //要拷贝的文件路径  
            string source = @"F:game[离线]Medieval.II.Total.War_CHS.HD-GamerskyMedieval.II.Total.War_CHS.HD-Gamersky.part01.rar";
            //目标路径  
            string target = @"e:1.rar";
            //测量运行时间  
            Stopwatch sw = new Stopwatch();
            sw.Start();
            //拷贝文件  
            CopyFile(source, target);
            sw.Stop();
            string times = DoubleToString(Convert.ToDouble(sw.ElapsedMilliseconds) / 1000);
            Console.WriteLine("总耗时:{0}秒。", times);
            Console.WriteLine("拷贝完成!!!");
            Console.ReadKey();
        }
        /// < summary>  
        /// 拷贝文件  
        /// < /summary>  
        /// <param name="source">源路径</param>  
        /// <param name="target">目标路径</param>  
        private static void CopyFile(string source, string target)
        {
            //Using 括号内的对象必须实现IDisposable接口的Dispose()方法  
            //Using 花括号内代码执行完成,会自动调用Using小括号内对象的Dispose()方法  
            //不管Using 花括号内是return,throw new Exception()都会执行,相当于try/catch中的finally  
            //创建一个读取文件流  
            using (FileStream fsRead = new FileStream(source, FileMode.Open))
            {
                //创建一个写入文件流  
                using (FileStream fsWrite = new FileStream(target, FileMode.Create))
                {
                    //创建一个读取、写入文件的缓冲区  10M大小  
                    byte[] buffer = new byte[1024 * 1024 * 10];
                    //获取读取文件的总长度  
                    long alllength = fsRead.Length;
                    long already = 0;
                    //开始读取文件  
                    while (true)
                    {
                        //读取文件流开始读文件,从0开始读,读取大小为缓冲区大小,读到的文件放到缓冲区中  
                        //返回值r表示实际读到的个数  
                        int r = fsRead.Read(buffer, 0, buffer.Length);
                        if (r <= 0)
                        {
                            //如果r<=0表示已无内容可读,此时结束循环  
                            break;
                        }
                        else
                        {
                            //否则表示读取内容,写入文件流将读取到的缓冲区内容写到文件中  
                            fsWrite.Write(buffer, 0, r);
                            //获得已写入文件的字节长度  
                            //第一种方式  
                            //already = fsWrite.Length;  
                            //第二种方式  
                            already = fsWrite.Position;
                        }
                        //显示进度  
                        if (already > 0)
                        {
                            Console.WriteLine("已完成{0}%", DoubleToString(Convert.ToDouble(already) / alllength * 100));
                        }
                    }
                }
            }
        }
        //double转换string,保留两位小数  
        public static string DoubleToString(double d)
        {
            return string.Format("{0:F}", d);
        }
    }
 

截图说明:

第一张:10M缓冲区 8s  第二张:100M缓冲区 21s

 

说明:缓冲区并不是越大越快,还和操作系统和硬件有关系。

 

 

 

 

 

原文地址:https://www.cnblogs.com/jiayue360/p/3166901.html