如何将文件夹加载到列表框中?

时间:2016-11-06 17:54:30

标签: c#

我试图将带有.mp3文件的文件夹加载到列表框中并能够播放它们,这是我尝试的代码,它加载文件夹但是doesent允许我播放文件

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);
    }
}

2 个答案:

答案 0 :(得分:0)

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

    foreach(string file in Files)
    {
        lsb.Items.Add(file);
    }
}

答案 1 :(得分: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();
           }
         }


      }
   }