Try replacing 'paths = ofd.FileNames' with something like this:
paths = ( paths ?? Enumerable.Empty<string>( ) ).Concat( ofd.FileNames ).ToArray( );
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi, I have created a media player in C#. This is the code for open Audio:
This opens and adds the audio file name and path to a list box
private void OpenAudio()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
file = ofd.SafeFileNames;
paths = ofd.FileNames;
for (int i = 0; i < file.Length; i++)
{
this.Width = 883;
listBox1.Items.Add(file[i]);
listBox1.SelectedIndex = -1;
}
}
}
Now this is the code when user double clicks an item:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
button3.Visible = false;
button1.Visible = true;
axWindowsMediaPlayer1.URL = paths[listBox1.SelectedIndex];
}
This code actually works perfectly when I firstly import a file.
Meaning:
If I import a file without previously importing any other previous files into the listbox, it plays the audio smoothly.
But when I add another file, with files already present in the listbox and then double click that new added file/ listbox item,
It throws this exception: 'Index was outside the bounds of the array.'.
Please help
Try replacing 'paths = ofd.FileNames' with something like this:
paths = ( paths ?? Enumerable.Empty<string>( ) ).Concat( ofd.FileNames ).ToArray( );
Hello,
Try this:
private List<String> Files = new List<string> { };
private List<String> Paths = new List<string> { };
private void OpenAudio()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
for (int i = 0; i < ofd.FileNames.Length; i++)
{
this.Width = 883;
Files.Add(System.IO.Path.GetFileNameWithoutExtension(ofd.FileNames[i]));
Paths.Add(ofd.FileNames[i]);
listBox1.Items.Add(Files[i]);
listBox1.SelectedIndex = -1;
}
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
button3.Visible = false;
button1.Visible = true;
axWindowsMediaPlayer1.URL = Paths[listBox1.SelectedIndex];
}