c# - 如何使用C#多选对话框中的特定文件?

标签 c# audio dialog

我需要使用从文件对话框中选择的特定文件。

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/

相关文章:

ios - iOS 中可用的输入/输出 AVAudioSession 数据源

c# - 设置对话框窗口的所有者时出现异常

android - 如何检测用户何时触摸对话框 fragment 外部?

c# - 为什么 TreeViewItem 中的项目继承父级的事件处理程序?

c# - xml c# 使用 Linq 添加元素到 Xml

c# - 如何在 C# 中找到 Unicode 字符的双向字符类型?

android - 从 Assets 文件夹获取mp3文件列表并保存为共享首选项

iphone - iPhone 上的音频

c++ - 使用 dialog_fselect 选择文件

c# - ADO.Net Entity Framework 事务