-1

im trying to load a folder with .mp3 files into a listbox and be able to play them, this is the code i have tried and it loads the folder but doesent allow me to play the files

private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
    DirectoryInfo dinfo = new DirectoryInfo(Folder);
    FileInfo[] Files = dinfo.GetFiles(FileType);
    foreach(FileInfo file in Files)
    {
        lsb.Items.Add(file.Name);
    }
}
Rok Povsic
  • 4,626
  • 5
  • 37
  • 53
MoDz Trolls
  • 37
  • 2
  • 5
  • Possible Duplicate for: http://stackoverflow.com/questions/15025626/playing-a-mp3-file-in-a-winform-application or http://stackoverflow.com/questions/3502311/how-to-play-a-sound-in-c-net – Timothy Ghanem Nov 06 '16 at 18:07
  • 1
    Invoke the magical play method `MagicPlay();` – Jim Nov 06 '16 at 18:07
  • I supposed you're using a dialog to select a folder and pass the full path to your method, have you tried with var files = System.IO.Directory.GetFiles("C:\temp\*.mp3"); instead of DirectoryInfo;? – H. Herzl Nov 06 '16 at 18:07

2 Answers2

0
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
    string[] files  = Directory.GetFiles(Folder);

    foreach(string file in Files)
    {
        lsb.Items.Add(file);
    }
}
Rafael Marques
  • 1,501
  • 15
  • 23
0
    //Try this.
    using System;
    using WMPLib;
    using System.IO;

    namespace _myNamespace
    {
      public partial class Form1:Form
      {
         //to use 'WindowsMediaPlayer' you need to import WMPLib
         //add a reference "Windows Media Player" from the COM section of vstudio reference manager.

         WindowsMediaPlayer player;
         //constructor
         public Form1
         {
           InitializeComponent();
           PopulateListBox("folder");
           player = new WindowsMediaPlayer();
         }

        private void PopulateListBox(string folder)
        { 
           string[] files = Directory.GetFiles(folder);
           foreach(string file in files)
              listbox1.Items.Add(file);
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
           string file = listbox1.SelectedItem as string;
           if(!string.IsNullOrWhiteSpace(file))
           {
              player.URL = file;
              player.controls.play();
           }
         }


      }
   }