簡體   English   中英

如何使用C#多選對話框中的特定文件?

[英]How do you use an specific file from a C# multiselection 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM