private string[] ColeccionDeCortes(string Path)
{
DirectoryInfo X = new DirectoryInfo(Path);
FileInfo[] listaDeArchivos = X.GetFiles();
string[] Coleccion;
foreach (FileInfo FI in listaDeArchivos)
{
//Add the FI.Name to the Coleccion[] array,
}
return Coleccion;
}
Chcel by som previesť FI.Name
na reťazec a potom ho pridať do môjho poľa. Ako to môžem urobiť?
Použiť List
List<string> myCollection = new List<string>();
…
myCollection.Add(aString);
Alebo skrátene (pomocou inicializátora kolekcie):
List<string> myCollection = new List<string> {aString, bString}
Ak naozaj chcete mať na konci pole, použite
myCollection.ToArray();
Možno by bolo lepšie abstrahovať na rozhranie, napríklad IEnumerable, a potom len vrátiť kolekciu.
Edit: Ak musíte použiť pole, môžete ho predalokovať na správnu veľkosť (t. j. počet FileInfo, ktoré máte). Potom v cykle foreach udržujte počítadlo pre index poľa, ktorý potrebujete aktualizovať ako ďalší.
private string[] ColeccionDeCortes(string Path)
{
DirectoryInfo X = new DirectoryInfo(Path);
FileInfo[] listaDeArchivos = X.GetFiles();
string[] Coleccion = new string[listaDeArchivos.Length];
int i = 0;
foreach (FileInfo FI in listaDeArchivos)
{
Coleccion[i++] = FI.Name;
//Add the FI.Name to the Coleccion[] array,
}
return Coleccion;
}