Chen Zen
Chen Zen

Reputation: 11

OpenFileDialog always display the first file opened

I'm trying to use OpenFileDialog to open multiple files and play them in a winforms media player COM component. Everything seem to work, though in the listbox I see the first file name times number of files selected.

_playList.Items.Clear();
string[] filenames = { };
_openFile.Multiselect = true;
_openFile.ShowDialog();
//filenames = _openFile.FileNames;
foreach (var name in _openFile.FileNames)
{
    string filename = System.IO.Path.GetFileName(_openFile.FileName);
    _playList.Items.Add(filename);                
}

Please advise.

Upvotes: 1

Views: 78

Answers (2)

Steve
Steve

Reputation: 216293

Your problem is caused by the error first pointed by HABO, however I wish to give also this answer because a ListBox.Items has a method called AddRange that can be used with a single line of code using Linq

_playList.Items.AddRange(_openFile.FileNames
                                  .Select (fn => Path.GetFileName(fn))
                                  .ToArray());

Upvotes: 0

HABO
HABO

Reputation: 15816

You need to use name from your foreach loop:

string filename = System.IO.Path.GetFileName(name);

Upvotes: 1

Related Questions