首页 > 解决方案 > 使用system.IO c#从A目录合并到B目录更新b目录

问题描述

我想将两个相等的目录 A 和 B 与子目录进行比较。如果我在 A 中更改、删除或包含目录或文件,我需要对 B 进行反思。是否有任何 C# 例程可以做到这一点?我有这个习惯,但我不能继续前进。我只想更新我在 A 到 B 中所做的更改,而不是全部。如果我在 A 中删除一个目录,它不会在 B 中删除

标签: c#system.io.directory

解决方案


获得每个目录中文件的完整列表后,您可以使用LINQ - Full Outer Join页面上记录的 FullOuterJoin 代码。如果您将每个键都设置为 DirA 和 DirB 下的相对路径以及其他字符,那么您将得到一个很好的比较。这将产生三个数据集 - A 中的文件但 B 中的文件,B 中的文件但 A 中的文件,以及匹配的文件。然后,您可以遍历每个并根据需要执行所需的文件操作。它看起来像这样:

public static IEnumerable<TResult> FullOuterJoin<TA, TB, TKey, TResult>(
    this IEnumerable<TA> a,
    IEnumerable<TB> b,
    Func<TA, TKey> selectKeyA,
    Func<TB, TKey> selectKeyB,
    Func<TA, TB, TKey, TResult> projection,
    TA defaultA = default(TA),
    TB defaultB = default(TB),
    IEqualityComparer<TKey> cmp = null)
{
    cmp = cmp ?? EqualityComparer<TKey>.Default;
    var alookup = a.ToLookup(selectKeyA, cmp);
    var blookup = b.ToLookup(selectKeyB, cmp);

    var keys = new HashSet<TKey>(alookup.Select(p => p.Key), cmp);
    keys.UnionWith(blookup.Select(p => p.Key));
    var join = from key in keys
               from xa in alookup[key].DefaultIfEmpty(defaultA)
               from xb in blookup[key].DefaultIfEmpty(defaultB)
               select projection(xa, xb, key);

    return join;
}

IEnumerable<System.IO.FileInfo> filesA = dirA.GetFiles(".", System.IO.SearchOption.AllDirectories); 
IEnumerable<System.IO.FileInfo> filesB = dirB.GetFiles(".", System.IO.SearchOption.AllDirectories); 
var files = filesA.FullOuterJoin(
    filesB,
    f => $"{f.FullName.Replace(dirA.FullName, string.Empty)}",
    f => $"{f.FullName.Replace(dirB.FullName, string.Empty)}",
    (fa, fb, n) => new {fa, fb, n}
);
var filesOnlyInA = files.Where(p => p.fb == null);
var filesOnlyInB = files.Where(p => p.fa == null);

// Define IsMatch - the filenames already match, but consider other things too
// In this example, I compare the filesizes, but you can define any comparison
Func<FileInfo, FileInfo, bool> IsMatch = (a,b) => a.Length == b.Length;
var matchingFiles = files.Where(p => p.fa != null && p.fb != null && IsMatch(p.fa, p.fb));
var diffFiles = files.Where(p => p.fa != null && p.fb != null && !IsMatch(p.fa, p.fb));
//   Iterate and copy/delete as appropriate

推荐阅读