Search code examples
c#visual-studiofile-uploadmultifile-uploader

Multiple file upload without full path


I'm trying to upload multiple files and just get the filename of them. When I'm trying to do that it just uploads one file.

So it uploads the files with the full path(And it works).

private void bChooseFolder_Click(object sender, EventArgs e)
{
    CoreClass.OPENDIALOG.Multiselect = true;
    string oldFilter = CoreClass.OPENDIALOG.Filter;
    CoreClass.OPENDIALOG.Filter = "(*.csv) | *.csv";

    if (CoreClass.OPENDIALOG.ShowDialog() == DialogResult.OK)
        tbFolderPath.Text = string.Join(FileSeperator, CoreClass.OPENDIALOG.FileNames);// <-- this works, but here I get the full path

    CoreClass.OPENDIALOG.Filter = oldFilter;
    CoreClass.OPENDIALOG.Multiselect = false;
}

And so I get just the Filename but it uploads just one File:

private void bChooseFolder_Click(object sender, EventArgs e)
{
    CoreClass.OPENDIALOG.Multiselect = true;
    string oldFilter = CoreClass.OPENDIALOG.Filter;
    CoreClass.OPENDIALOG.Filter = "(*.csv) | *.csv";

    if (CoreClass.OPENDIALOG.ShowDialog() == DialogResult.OK)
        tbFolderPath.Text = string.Join(FileSeperator, System.IO.Path.GetFileNameWithoutExtension(CoreClass.OPENDIALOG.FileName)); // <-- Doesn't work. Just one File.

    CoreClass.OPENDIALOG.Filter = oldFilter;
    CoreClass.OPENDIALOG.Multiselect = false;
}

Solution

  • OK, If you are developing WinForms app then you are using OpenFileDialog which contains 2 properties:

    • FileName gets or sets a string containing the file name selected in the file dialog box.
    • FileNames gets the file names of all selected files in the dialog box.

    Then first one will never contains few files and you should use it only in Multiselect = false; mode.

    If you need to show all file names in one textbox then you can use String.Join method and LINQ to enumerate collection and get file name without extension for each element:

    if (CoreClass.OPENDIALOG.ShowDialog() == DialogResult.OK)
        tbFolderPath.Text = string.Join(FileSeperator, CoreClass.OPENDIALOG.FileNames.Select(x => System.IO.Path.GetFileNameWithoutExtension(x)).ToArray()); // <-- Doesn't work. Just one File.