这是把别人写的代码稍微修改了一下,但是不能使用
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
namespace mplayer
{
public partial class mplayer: Form
{
private string []playlist=new string[10000];
private int num;
public void AddFile(string path)
{ if(num<10000)
{
num++;
playlist[num]=path;
}
}
private void DelFile(int selectnum)
{
for(int i=selectnum;i<num-1;i++)
{
playlist[i]=playlist[i+1];
}
num--;
}
private void AddFiles(string path, ListBox listBox1)
{
DirectoryInfo dir=new DirectoryInfo(path);
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)
{
MediaPlayer1.URL=playlist[selectnum];
}
public mplayer()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.SelectedPath = "f:\\"; //设置初始打开路径
folderBrowserDialog1.ShowNewFolderButton = true;
folderBrowserDialog1.Description = "请选择音乐文件目录:"; // 显示新建文件夹按钮
folderBrowserDialog1.ShowDialog();
AddFiles(folderBrowserDialog1.SelectedPath, listBox1); //调用AddFiles方法
}
private void mplayer_Load(object sender, EventArgs e)
{
listBox1.Items.CopyTo(playlist, 0);
num = 0;
}
private void button3_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*.mp3|*.mp3";
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 button4_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 button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
DelFile(listBox1.SelectedIndex + 1); //调用DelFile方法
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}
private void button5_Click(object sender, EventArgs e)
{
MediaPlayer1.URL = "";
}
private void button6_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}