Merging 2 Text Files in C#

后端 未结 4 574
迷失自我
迷失自我 2020-12-21 01:10

Firstly, i\'d just like to mention that I\'ve only started learning C# a few days ago so my knowledge of it is limited.

I\'m trying to create a program that will par

4条回答
  •  [愿得一人]
    2020-12-21 02:02

    Using a FileInfo extension you could merge one or more files by doing the following:

    public static class FileInfoExtensions
    {
      public static void MergeFiles(this FileInfo fi, string strOutputPath , params string[] filesToMerge)
      {
        var fiLines = File.ReadAllLines(fi.FullName).ToList();
        fiLines.AddRange(filesToMerge.SelectMany(file => File.ReadAllLines(file)));
        File.WriteAllLines(strOutputPath, fiLines.ToArray());
      }
    }
    

    Usage

      FileInfo fi = new FileInfo("input");
      fi.MergeFiles("output", "File2", "File3");
    

提交回复
热议问题