2

I have a list of music files in my listbox and I'm trying to play them using the media player when I double click on them. But it keeps coming up with the error. I'm assuming it's because 'files' is not a string, but how do I convert it into a string? I've tried using .ToString but it doesn't work. I'm quite new to this. Any help is appreciated.

The error is in the axWindowsMediaPlayer.URL = files[listBox1.SelectedIndex];

Here is my code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    string[] extensions = new[] { ".mp3", ".wma", ".wav", ".MP3", ".WMA" };

    FileInfo[] files;

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex];
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        DirectoryInfo dinfo = new DirectoryInfo(@"C:\Tracks");
        files = dinfo.EnumerateFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();

        for (int i = 0; i < files.Length; i++)
        {
            listBox1.Items.Add(files[i]);
        }


    }

This is the code that's showing the error:

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex];
}
mason22
  • 35
  • 2
  • 6
  • `dinfo.EnumerateFiles` returns a collection of `FileInfo` objects, which you stuff into a listbox. So what is the mystery? – Ňɏssa Pøngjǣrdenlarp Apr 16 '18 at 17:26
  • Can you post *exactly* the code you tried which did not work? – BJ Myers Apr 16 '18 at 17:27
  • 2
    This is [FileInfo](https://msdn.microsoft.com/en-us/library/system.io.fileinfo_properties(v=vs.110).aspx) so you're probably looking for the `FullName` property of that instance. I'm not sure if your cose will work after that change as FullName doesn't contain an URL. But maybe you're lucky. At least you fixed the compile error. – rene Apr 16 '18 at 17:27
  • *"how do I convert it into a string?"* - That depends on what you want that string to *be*. You have a `FileInfo` object. When you display that object as a string, what do you expect it to show you? – David Apr 16 '18 at 17:28

3 Answers3

2

I don't have an IDE open to try this, but try changing:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex];
}

to:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex].Fullname;
}
Phil N DeBlanc
  • 398
  • 1
  • 13
1

FileInfo.FullName if you want the full path of the file.

FileInfo.Name if you just want the name.

In your code:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex].FullName;
}
Sergio Monteleone
  • 2,776
  • 9
  • 21
0

You need to provide the filepath to the AxWindowsMediaPlayer.URL Attribute You can fetch it with the FileInfo.FullName Attribute.

So use axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex].FullName;

Canonip
  • 61
  • 6