读书人

C#文件大小对照

发布时间: 2012-08-26 16:48:05 作者: rapoo

C#文件大小对比

 /// <summary>        ///  根据两个文件路径,实现文件同步,如果文件大小不一致,则以源文件覆盖目标文件        /// </summary>        /// <param name="src">源文件</param>        /// <param name="dest">目标文件</param>        public static void ContrastFile(string src, string dest)        {            Console.WriteLine(DateTime.Now.ToString() + " 正在检测文件一致性...");            long p1Len = 0;            long p2Len = 0;            if (!File.Exists(dest))     // 如果第二个路径不存在就创建了            {                string con = "hello,world";            //    Console.WriteLine("创建临时文件:{0}", dest);                using (StreamWriter sw = File.CreateText(dest))                {                    sw.WriteLine(con);            //        Console.WriteLine("写入数据:{0}", con);                }            }            if (File.Exists(src) && File.Exists(dest)) // 文件对比            {                FileInfo f1 = new FileInfo(src);                FileInfo f2 = new FileInfo(dest);                // 判断文件大小                 p1Len = f1.Length;                p2Len = f2.Length;        //        Console.WriteLine("  文件大小(bytes) \n  网络文件:{0},目标文件:{1}", p1Len, p2Len);                if (p1Len != p2Len) // 删掉第二个,复制第一个到第二个位置                {                    File.Delete(dest);       //             Console.WriteLine("  删除文件 : {0}", dest);                    File.Copy(src, dest);       //             Console.WriteLine("  替换文件 : {0} -- {1} \n  文件替换成功!", src, dest);                     Console.WriteLine(DateTime.Now.ToString() + " 【文件不一致,已经更新为最新版。】 ");                }                else                {                    Console.WriteLine(DateTime.Now.ToString() + " 【已经是最新版本,保持不变。】");                }            }        } 

?

读书人网 >C#

热点推荐