我需要使用从文件对话框中选择的特定文件。
OpenFileDialog ofd = new OpenFileDialog();
private void pictureBox23_Click(object sender, EventArgs e)
{
ofd.Filter = "WAV|*.wav";
this.ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
label23.Text = ofd.SafeFileName;
}
else
{
label23.Text = "No files selected...";
}
}
我需要做的是选择并使用我预定义的文件,因此,如果用户选择一个名为01.wav的文件,则如果我用01.wav定义了一个事件,则将这样使用该文件:
using (SoundPlayer player = new SoundPlayer(Beatpadpc.Properties.Resources._01))
{
player.Play();
}
我目前已对其进行了调整,因为它将从资源中播放它,我需要做的是从文件选择中播放文件,但前提是文件名为“01” .wav
有办法吗?
最佳答案
好吧,只过滤名为Filenames的ofd属性。
OpenFileDialog ofd = new OpenFileDialog();
string strAudioFilePath = String.Empty;
private void pictureBox23_Click(object sender, EventArgs e)
{
ofd.Filter = "WAV|*.wav";
this.ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
// Here you make a small filter with linq
label23.Text = strAudioFilePath = ofd.FileNames.FirstOrDefault(x => x.EndsWith("01.wav")); // you change 01.wav to something that make more sense to you
}
else
{
label23.Text = "No files selected...";
}
}
然后,如果要删除资源文件,只需为声音播放器提供文件路径即可,仅此而已。
SoundPlayer simpleSound = new SoundPlayer(strAudioFilePath);
simpleSound.Play();
关于c# - 如何使用C#多选对话框中的特定文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14015702/