zoukankan      html  css  js  c++  java
  • 根据新旧文件版本或者时间对比,抽取更新文件

    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;
            }
    
        }
    }
  • 相关阅读:
    HDU 3833 YY's new problem ()
    从文件读入16进制数转化为10进制数再输出到文件中
    UESTC 1215 (思维题 旋转)
    HDU2067卡特兰数
    HDU2050离散数学折线分割平面
    cshell学习
    C++学习1
    QT学习1
    QT Creator常用快捷键
    Ubuntu14.04安装QT5.5
  • 原文地址:https://www.cnblogs.com/vincentvoid/p/12189295.html
Copyright © 2011-2022 走看看