How to select more than one txt file with C#

Asked

Viewed 264 times

1

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

My question is in this import of the files, 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());
        }
    }

1 answer

3


Just set up the property Multiselect. The result will be returned in the property 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));
}

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.