3
I’m doing a check in a directory where I list all items with a specific extension.
And I need to add the items into one array through a foreach
.
Follow part of the code I’ve tried.
DirectoryInfo di = new DirectoryInfo(_caminhoEmail);
FileInfo[] rgFiles = di.GetFiles("*.ost");
string[] pasta = new string[] { };
foreach (FileInfo item in rgFiles)
{
pasta = new string[] { item.FullName};
}
return pasta;
But with this code it always overrides taking only the last value of the array in rgFiles[]
.
How do I add items to Array pasta
and return it at the end?
FileInfo[] rgFiles = di.GetFiles("*.ost");
here it already brings you an array of information, can not use this?– novic
The mistake is here:
pasta = new string[] { item.FullName };
. You create a new instance by overriding the previous one. Do withforeach
It does, but it’s not the right thing for what you need, it would be too much unnecessary code. I recommend evaluating @Virgilionovic’s comment and response– Leandro Amarilha