根据新旧文件版本或者时间对比,抽取更新文件

Posted vincentvoid

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据新旧文件版本或者时间对比,抽取更新文件相关的知识,希望对你有一定的参考价值。

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            FileCompare com = new FileCompare();
            com.StartCompare();
        }
    }


    public class FileCompare
    {
        public DateTime JieZhiDate = DateTime.Parse("2020-01-01"); //取出修改时间大于此时间的文件
        string floadPath1 = @"C:AUpdateTool";
        string floadPath2 = @"C:AUpdateTool";
        string floadPath3 = @"C:TEST";


        /// <summary>
        /// 比较按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void StartCompare()
        {

            object[] strArr = new object[] { floadPath1, floadPath2, floadPath3 };
            Thread thread = new Thread(new ParameterizedThreadStart(StartCheck));
            thread.Start(strArr);
        }

        /// <summary>
        /// 同步文件版本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnTongBu_Click()
        {

            object[] strArr = new object[] { floadPath1, floadPath3, floadPath1 };
            Thread thread = new Thread(new ParameterizedThreadStart(StartTongBu));
            thread.Start(strArr);
        }

        /// <summary>
        /// 抽取更新文件
        /// </summary>
        /// <param name="obj"></param>
        public void StartCheck(object obj)
        {
            object[] path = obj as object[];
            string strSource = path[0].ToString();    //老的文件
            string strCurren = path[1].ToString();    //新文件
            string strTarget = path[2].ToString();    //需要更新的文件


            #region 删除更新文件

            if (Directory.Exists(floadPath3))
            {
                try
                {
                    Directory.Delete(floadPath3, true);  //删除更新文件夹中文件
                    Directory.CreateDirectory(floadPath3);
                }
                catch { }

            }

            #endregion

            CheckFile(strSource, strCurren, strTarget);  //检查本目录文件

            CheckSonFolds(strSource, strCurren, strTarget);  //检查子目录更新文件
        }

        /// <summary>
        /// 同步
        /// </summary>
        /// <param name="obj"></param>
        public void StartTongBu(object obj)
        {
            object[] path = obj as object[];
            string strSource = path[0].ToString();    //需要更新的文件目录
            string strCurren = path[1].ToString();    //老的文件目录
            string strTarget = path[2].ToString();    //需要更新的文件目录


            CheckFile(strSource, strCurren, strTarget);  //检查本目录文件

            CheckSonFolds(strSource, strCurren, strTarget);  //检查子目录更新文件



        }

        /// <summary>
        /// 检查子目录更新文件
        /// </summary>
        /// <param name="strSource">源目录</param>
        /// <param name="strCurren">新目录</param>
        /// <param name="strTarget">COPY目标</param>
        public void CheckSonFolds(string strSource, string strCurren, string strTarget)
        {
            #region 搜索当前文件夹

            string[] strNewFolds = GetSonFload(strCurren);

            if (strNewFolds != null && strNewFolds.Length > 0)
            {
                for (int i = 0; i < strNewFolds.Length; i++)
                {
                    string path1 = strNewFolds[i].Replace(strCurren, strSource);
                    string path2 = strNewFolds[i];
                    string path3 = strTarget + strNewFolds[i].Replace(strCurren, "");

                    CheckFile(path1, path2, path3);  //检查文件

                    CheckSonFolds(path1, path2, path3);  //检查文件夹
                }
            }

            #endregion
        }

        /// <summary>
        /// 检查文件
        /// </summary>
        /// <param name="strSource">老目录</param>
        /// <param name="strCurren">新目录</param>
        /// <param name="strTarget">Copy目录</param>
        public void CheckFile(string strSource, string strCurren, string strTarget)
        {
            #region 检查当前目录文件
            string[] strNewFiles = GetCheckFile(strCurren);
            if (strNewFiles != null && strNewFiles.Length > 0)
            {
                for (int i = 0; i < strNewFiles.Length; i++)
                {
                    string path1 = strNewFiles[i].Replace(strCurren, strSource);
                    string path2 = strNewFiles[i];

                    //bool flag = Compare(path1, path2);//比较两个文件差异
                    //if (flag)
                    //{

                    //    if (!FilterFile(path2))    //判断文件不是需过滤文件
                    //    {
                    //        Copy(path2, strTarget);
                    //    }
                    //}
                    bool flag = Compare(path1);   //根据时间判断是否需要复制
                    if (flag)
                    {

                        if (!FilterFile(path1))    //判断文件不是需过滤文件
                        {
                            Copy(path1, strTarget);
                        }
                    }
                }
            }
            #endregion
        }

        /// <summary>
        /// 查找当前路径下的文件
        /// </summary>
        /// <param name="currentPath"></param>
        public string[] GetCheckFile(string currentPath)
        {
            if (!Directory.Exists(currentPath))
                return null;

            return Directory.GetFiles(currentPath);
        }

        /// <summary>
        /// 取出该路径所有的子目录
        /// </summary>
        /// <param name="currentPath"></param>
        /// <returns></returns>
        public string[] GetSonFload(string currentPath)
        {
            if (!Directory.Exists(currentPath))
                return null;

            return Directory.GetDirectories(currentPath);
        }


        public bool Compare(string file1)
        {

            bool result = false;
            if ("" == file1)
                return false;

            if (!File.Exists(file1))
                return false;

            FileInfo fi = new FileInfo("E:\text.txt");
            if (fi.LastWriteTime > JieZhiDate)
            {
                return true;
            }

            return result;
        }

        /// <summary>
        /// 比较两个文件是否相等, 不相同返回True,相同返回Flase
        /// </summary>
        /// <param name="file1"></param>
        /// <param name="file2"></param>
        /// <returns></returns>
        public bool Compare(string file1, string file2)
        {
            bool result = false;
            if ("" == file1 || "" == file2)
                return false;

            if (!File.Exists(file1))
                return true;

            StreamReader sr1 = new StreamReader(file1);
            StreamReader sr2 = new StreamReader(file2);

            try
            {
                if (object.Equals(sr1.ReadToEnd(), sr2.ReadToEnd()))        //读取两个文件的内容并判断是否相同
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
            }
            catch
            {

            }
            finally
            {

                sr1.Dispose();
                sr2.Dispose();
            }

            return result;
        }

        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="newFile">新文件</param>
        /// <param name="targetPath">目标文件夹</param>
        public void Copy(string newFile, string targetPath)
        {
            string file = newFile.Substring(newFile.LastIndexOf(\));

            if (!Directory.Exists(targetPath))
                Directory.CreateDirectory(targetPath);

            string newFileName = targetPath + file;

            File.Copy(newFile, newFileName, true);

            FileInfo fileInfoSet = new FileInfo(newFileName);
            //去掉只读属性 
            fileInfoSet.Attributes &= ~FileAttributes.ReadOnly;

        }



        /// <summary>
        /// 过滤成功返回True,否则返回False
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public bool FilterFile(string filePath)
        {
            bool result = false;

            if (string.IsNullOrEmpty(filePath))
                return true;

            int index = filePath.LastIndexOf(.);
            if (index < 0)
                return true;

            string strSuffixs = filePath.Substring(index + 1);

            IList<string> list = GetSuffixs();
            if (list.Contains(strSuffixs))
                return true;

            return result;
        }

        public IList<string> GetSuffixs()
        {
            IList<string> list = new List<string>();
            list.Add("pdb");
            return list;
        }

    }
}

 

以上是关于根据新旧文件版本或者时间对比,抽取更新文件的主要内容,如果未能解决你的问题,请参考以下文章

一个diff工具,用于判断两个目录下所有的改动(比较新旧版本文件夹)

git在merge后会不会把最新代码更新过来

更新时获取文档的新旧版本

CSS Flex 新旧法语对比

移动端测试:优化原有功能,改动接口需要兼容新旧版本

从代码 c# 更新应用程序版本号