Save in an Array [] file names of a folder in C #

-1

How to save the file names in an array? When opening the Open File Dialog the path is obtained and in an array all the names of images or files are saved.

Thank you.

    
asked by Gonzalo Rios 28.11.2018 в 23:27
source

2 answers

3

Use the method GetFiles() of class Directory .

This will return an array with all the files that match the search criteria, for example:

ruta = @"c:\users\juan\documents";
string[] archivos = Directory.GetFiles(ruta, "*.jpg");

It will return all files with .jpg extension that are in my document folder.

    
answered by 28.11.2018 / 23:40
source
0

You can not use OpenFileDialog to get a route, for that the FolderBrowserDialog

is used
string[] arrayNombres;

using(var folderBrowserDialog1 = new FolderBrowserDialog())
{
    DialogResult result = folderBrowserDialog1.ShowDialog();
    if( result == DialogResult.OK )
    {
        string folderName = folderBrowserDialog1.SelectedPath;

        arrayNombres = Directory.GetFiles(folderName, "*.*");
    }
}

Using Directory.GetFiles() you get the files in the folder

How to: Choose folders with the FolderBrowserDialog component of Windows Forms

    
answered by 28.11.2018 в 23:40