Get FileStream from N files

1

Hello everyone, how can I save the stream of files stored in a folder in an array? This is my code:

string ruta = @"C:\";
foreach (string s in Directory.GetFiles(ruta))
{
 //Obtengo en (s) todos los archivos almacenado en la carpeta
 FileStream filestr= File.OpenRead(s);    
 }

I save the stream of the file s in the filestr variable, but it will change as I go through the foreach, there is some way to save the stream of each file, in an arrangement to later use it.

    
asked by Rastalovely 23.01.2017 в 20:41
source

1 answer

3

You could create a FileStream array to assign each reading

List<FileStream> list = new List<FileStream>();

string ruta = @"C:\";
foreach (string s in Directory.GetFiles(ruta))
{
    FileStream filestr = File.OpenRead(s);   
    list.Add(filestr);
}

I also do not think it is recommended to keep many open files in memory, because remember that they will be blocked while they are still reading

Note: do not define as path c: \ es for problems

> > but how can I close the filestream

If you have the list you can iterate it to close each open file

foreach(var file in list){
  file.Close();
}
    
answered by 24.01.2017 / 05:54
source