Read Files Simultaneously

By Peter Bromberg

A simple technique to read files simultaneously using a string array of file names

public static void ReadFilesSimultaneously(params string[] filesToRead)
      {
           StreamReader[] readers = filesToRead.Select(fileToRead => new StreamReader(fileToRead)).ToArray();
           StringBuilder result = new StringBuilder();
            int lineNumber = 1;
             while (readers.Any(reader => reader.Peek() != -1))
    {
                 result.Clear();
                 result.AppendFormat("Line #{0}", lineNumber++);
                 for (int index = 0; index < readers.Length; index++)
                     result.AppendFormat("; {0}:{1}", Path.GetFileName(filesToRead[index]), readers[index].Peek() == -1 ? "[EOF]" : readers[index].ReadLine());
                 MessageBox.Show(result.ToString());
             }
             foreach (StreamReader reader in readers)
    {
                 reader.Close();
                 reader.Dispose();
            }
        }

Read Files Simultaneously  (2043 Views)