views:

1360

answers:

5
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;
        }

I'd like to convert the FI.Name to a string and then add it to my array.

How can I do this?

+2  A: 

Use List<T> from System.Collections.Generic

List<string> myCollection = new List<string>();

…

myCollection.Add(aString);

If you really want an array at the end, use

myCollection.ToArray();

You might be better off abstracting to an interface, such as IEnumerable, then just returning the collection.

Edit: If you must use an array, you can preallocate it to the right size (i.e. the number of FileInfo you have). Then, in the foreach loop, maintain a counter for the array index you need to update next.

    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;
    }
Adam Wright
My method to join recieves only a string[] array, I can't use a List<>. Is there a way for me to solve this using an Array?
Sergio Tapia
Use a List<string> and then when you need the array, call the ToArray method
Chris Dunaway
+14  A: 

You can't add items to an array, since it has fixed length, what you're looking for is a List<string>, which can later be turned to an array using list.ToArray().

Saulius
+1 I'll go ahead and offer a link to my answer on this subject. http://stackoverflow.com/questions/1168915/which-one-is-more-effecient-listint-or-int/1168922#1168922
280Z28
+1  A: 

You don't need all that code. It's a one-liner with Linq (the input param 'Path' in your example becomes 'inputPath' in this query):

string[] coleccion = (from string fullPath in Directory.GetFiles(inputPath) 
    select new FileInfo(fullPath).Name).ToArray();
xcud
+1  A: 

Alternatively, you can resize the array.

Array.Resize(ref array, array.Length + 1);
array[array.Length - 1] = "new string";
Siebe Tolsma
`Array.Resize` *is* the proper way to resize an array. If you add a comment before the code snippet saying it's rarely the best way to handle situations where the array represents a resizable collection you've got a +1. :)
280Z28
A: 

I would not use an array in this case. Instead I would use a StringCollection.

using System.Collections.Specialized;

private StringCollection ColeccionDeCortes(string Path)
{

DirectoryInfo X = new DirectoryInfo(Path);

FileInfo[] listaDeArchivos = X.GetFiles();
StringCollection Coleccion = new StringCollection();

foreach (FileInfo FI in listaDeArchivos)
{
  Coleccion.Add( FI.Name );
}
return Coleccion;

}

Miguel Martinez