I want to list multiple files in a directory, and I need to merge every 10 files.
public static void MergeFiles (string pathSource)
{
DirectoryInfo diretorio = new DirectoryInfo(pathSource);
FileInfo[] Arquivos = diretorio.GetFiles("*.*");
int i = 0;
foreach (FileInfo file in Arquivos)
{
StreamReader[] writer = **new StreamReader();** //'System.IO.StreamReader' does not contain a constructor that takes 0 arguments
writer[i] = File.OpenText(diretorio + file.Name);
Console.WriteLine(writer.Count());
i++;
}
}
How can I resolve this problem?
An array is fixed size, and should be initialized as follows (please don't call a reader "writer"):
// Size of 10
StreamReader[] writer = new StreamReader[10];
Also you are creating the array inside the loop, meaning there will be one array per file. Create the array outside of the loop:
StreamReader[] writer = new StreamReader[10];
foreach (FileInfo file in Arquivos)
{
...
You may consider using a List<StreamReader>
so if you have say 25 files, you would be able to merge 10, 10 and finally the remaining 5 files:
List<StreamReader> readers = new List<StreamReader>();
foreach (FileInfo file in Arquivos)
{
readers.Add(File.OpenText(diretorio + file.Name));
if (readers.Count >= 10)
{
MergeFiles(readers);
readers.Clear();
}
}
// Optionally handle the last bunch
if (readers.Count > 0)
{
MergeFiles(readers);
}