How to select more than one txt file with C #

1

I am writing a C # application that needs to receive more than 1 text file and display the Filename of them in an individual MessageBox for each.

My question is in this file import, I was using OpenFileDialog to select the file, but it does not work if I select more than 1 text file, below the code used:

private void btnSelecionarArquivos_Click(object sender, EventArgs e)
    {
        OpenFileDialog fDialog = new OpenFileDialog();
        fDialog.Filter = "Arquivos Texto|*.txt";
        fDialog.Title = "Selecione os Arquivos";
        if(fDialog.ShowDialog() == DialogResult.OK)
        {
            MessageBox.Show(fDialog.FileName.ToString());
        }
    }
    
asked by anonymous 16.03.2016 / 18:52

1 answer

3

Just set the Multiselect property % . The result will be returned in the Filenames (plural) which is a array of strings :

private void btnSelecionarArquivos_Click(object sender, EventArgs e) {
    OpenFileDialog fDialog = new OpenFileDialog();
    fDialog.Multiselect = true;
    fDialog.Filter = "Arquivos Texto|*.txt";
    fDialog.Title = "Selecione os Arquivos";
    if(fDialog.ShowDialog() == DialogResult.OK) {
        MessageBox.Show(fDialog.FileNames.Aggregate((atual, proximo) => atual + ", " + proximo));
    }
}
    
16.03.2016 / 19:04