string[] path;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();//打开对话框
file.Title = "选择文件"; //对话框标题
file.Multiselect = true; //可以多选文件
file.Filter = "音乐文件|*.wav|所有文件|*.*";
file.InitialDirectory = @"G:\"; //默认盘符
file.ShowDialog();
path = file.FileNames; ; //获取文件路径 【包括文件名+扩展名】
for (int i = 0; i < path.Length; i++)
{
listBox1.Items.Add(Path.GetFileName(path[i])); //将Listbox的选项内容设置为所获取的文件名+扩展名
}
}
SoundPlayer playMusic = new SoundPlayer();
private void listBox1_DoubleClick(object sender, EventArgs e)
{
playMusic.SoundLocation = path[listBox1.SelectedIndex];//根据Listbox中的索引 找到对应的文件路径
playMusic.Play();
C# 十六章(OpenFileDialog 练习 线程Thread练习 鼠标 键盘事件 )
最新推荐文章于 2022-05-12 10:12:44 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)