using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Ex03_1
{
    public partial class Form1 : Form
    {

        private string[] playList = new string[10000];    //字符串数组
        private int num;         //MP3歌曲的个数

       
        
        
        public void AddFile(string path)        //自定义AddFile方法
        {
            if (num < 10000)                //歌曲数量不能超过
            {
                num++;
                playList[num] = path;
            }
        }

        public void DelFile(int selectNum)            //自定义DelFile方法
        {
            for (int i = selectNum; i <= num - 1; i++)
            {
                playList[i] = playList[i + 1];
            }
            num--;
        }

        private void AddFiles(string path, ListBox listBox1)     //自定义AddFiles方法   
        {
            DirectoryInfo dir = new DirectoryInfo(path);       //定义DirectoryInfo实例
            foreach (FileInfo f in dir.GetFiles("*.mp3"))
            {
                AddFile(f.FullName);
                string strTmp = Convert.ToString(num);
                for (int i = 1; i <= 5 - strTmp.Length; i++)
                    strTmp += ' ';
                strTmp += "--" + f.Name;
                this.ListBox1.Items.Add(strTmp);
            }
            foreach (DirectoryInfo f in dir.GetDirectories())
            {
                AddFiles(f.FullName, listBox1);
            }
        }

        public void PlaySong(int selectNum)                     //自定义播放多媒体PlaySong方法
        {
            MediaPlayer1.URL = playList[selectNum];
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ListBox1.Items.CopyTo(playList, 0);
            num = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = "d:\\";　　　//设置初始打开路径
            folderBrowserDialog1.ShowNewFolderButton = true;
            folderBrowserDialog1.Description = "请选择音乐文件目录："; // 显示新建文件夹按钮
            folderBrowserDialog1.ShowDialog();
            AddFiles(folderBrowserDialog1.SelectedPath, ListBox1);    //调用AddFiles方法

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)   //调用打开文件对话框
            {
                string path = this.openFileDialog1.FileName;
                FileInfo f = new FileInfo(path);
                AddFile(f.FullName);
                string strTmp = Convert.ToString(num);
                for (int i = 1; i <= 5 - strTmp.Length; i++)
                    strTmp += ' ';
                strTmp += "--" + f.Name;
                this.ListBox1.Items.Add(strTmp);                    //在列表框中显示添加的歌曲
            } 

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int SelectOne;
            if (ListBox1.SelectedIndex < 0)
                SelectOne = 1;
            else
                SelectOne = ListBox1.SelectedIndex + 1;

            if (ListBox1.Items.Count < 0)
                ListBox1.SelectedIndex = 0;
            PlaySong(SelectOne);                             //播放当前选择歌曲

        }

        private void button4_Click(object sender, EventArgs e)
        {
            MediaPlayer1.URL = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex >= 0)
            {
                DelFile(ListBox1.SelectedIndex + 1);                //调用DelFile方法
                ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
            }
        }
    }
}
