首页 > 解决方案 > 如何通过声音播放器传递字符串变量?

问题描述

我正在尝试将文件位置从我的列表框中传递到我的声音播放器中。

string fileLocation = listBox1.ToString();
SoundPlayer splayer = new SoundPlayer(soundLocation: fileLocation);
splayer.Play();

调试将变量显示为“C:\Temp\Sample1.wav”。

错误返回为“System.NotSupportedException:'不支持给定路径的格式。'”

谁能告诉我我在这里做错了什么?

标签: c#

解决方案


破解它!您需要拉出对象,然后将其转换为字符串。

        object fileLocationobj = listBox1.Items[0];
        string fileLocation = fileLocationobj.ToString(); 
        SoundPlayer splayer = new SoundPlayer(soundLocation: fileLocation);
        splayer.Play()

推荐阅读