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:\A\UpdateTool\";
string floadPath2 = @"C:\A\UpdateTool\";
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;
}
}
}