1

私はC#で2つのテキストファイルを持っています: File - A& File - B. 両方のファイルの内容を比較したいのですが、 に存在File - Aしないコンテンツが見つかった場合はFile - B、そのコンテンツをファイル B の同じ場所に配置したいと考えていますFile - A

   File - A                                        File - B

This is the example text.                         This is text.

上記の 2 つのファイルの内容を比較すると、出力は次のようになります。

                           File - B

                       This is the example text.

それで、私のためにこれを行うことができるc#の方法があれば教えてください。

4

4 に答える 4

2
var f1 = File.ReadAllLines(@"c:\temp\l1.txt");
var f2 = File.ReadAllLines(@"c:\temp\l3.txt");

var result = f1.Select((l, index) => new {Number= index, Text = l})
  .Join(f2.Select((l, index) => new {Number= index, Text = l}), 
        inner => inner.Number, 
        outer => outer.Number, 
        (inner, outer) =>  {
        if(inner.Text == "")
            return outer.Text;
        return inner.Text;
  })
  .Concat(f1.Where((l, index) => index >= f2.Count()))
  .Concat(f2.Where((l, index) => index >= f1.Count()));
  //.Dump();

File.WriteAllLines(@"c:\temp\l3.txt", result);

これは行ごとに比較し、最初のファイルの行が空の場合は 2 番目のファイルの行を保持し、それ以外の場合は常に最初のファイル行を出力します....

次に、結果を両方のファイルの左の行と連結します。

于 2012-06-06T13:55:16.127 に答える
1

これを試して:

 string fileAContent = File.ReadAllText(fileAPath);
 string fileBContent = File.ReadAllText(fileBPath);

 string[] fileAWords = filesAContent.split(_your delimiters_);
 string[] fileBWords = filesBContent.split(_your delimiters_);

 if (fileAWords.Except(fileBWords).Length > 0)
 {
    // there are words in file B that are not in file A
 }

パフォーマンスを最適化したい場合は、fileAWords のすべての単語を HashSet に追加してから、すべての fileBWords を繰り返し処理し、ハッシュセットに存在しない作業があるかどうかを確認します。

于 2012-06-06T13:51:38.517 に答える
1

シンプルな LINQ アプローチ:

var file1 = File.ReadLines(path1);
var file2 = File.ReadAllLines(path2);
var onlyInFileA = file1.Except(file2);
File.WriteAllLines(path2, file2.Concat(onlyInFileA));
于 2012-06-06T15:21:38.963 に答える
1

要件の完全なセットは実際にはありませんが、ここにあります。

string[] fileAWords = File.ReadAllText("C:\\File - A.txt").Split(' ');
string[] fileBWords = File.ReadAllText("C:\\File - B.txt").Split(' ');

// The comparer makes it so the union is case insensitive
// For example: Welcome in File - A and welcome (lower-case) in File - B in a Union would both be in the result
// With the comparer, it will only appear once.
IEnumerable<string> allWords = fileAWords.Union(fileBWords, new StringEqualityComparer());

// We did the split on a space, so we want to put the space back in when we join.
File.WriteAllText("C:\\File - C.txt", string.Join(" ", allWords));

StringEqualityComparer クラスのコードは次のとおりです。

class StringEqualityComparer : IEqualityComparer<string>
{
    // Lower-case your strings for a case insensitive compare.
    public bool Equals(string s1, string s2)
    {
        if (s1.ToLower().Equals(s2.ToLower()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    #region IEqualityComparer<string> Members
    public int GetHashCode(string s)
    {
        return s.GetHashCode();
    }

    #endregion
}
于 2012-06-06T13:43:20.800 に答える