如何播放 Windows 通知声音?(它没有在 System.Media.SystemSounds 中定义)

FNG*_*FNG 4 c# windows audio

使用 System.Media 时,有一种叫做 SystemSounds 的东西,您可以在其中轻松播放几种操作系统声音:

System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Hand.Play();
System.Media.SystemSounds.Question.Play();
Run Code Online (Sandbox Code Playgroud)

不幸的是,只有这五个选项,而在 Windows 10 中,其中三个是相同的,而其中一个甚至不播放任何内容。

我真正想做的是播放“声音”面板中定义的“通知”声音(见此处):

(imgur:声音面板中的通知声音)

有谁知道如何做到这一点?

FNG*_*FNG 5

找到解决方案。代码在这里:

using System.Media;
using Microsoft.Win32;

public void PlayNotificationSound()
{
    bool found = false;
    try
    {
        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\.Default\Notification.Default\.Current"))
        {
            if (key != null)
            {
                Object o = key.GetValue(null); // pass null to get (Default)
                if (o != null)
                {
                    SoundPlayer theSound = new SoundPlayer((String)o);
                    theSound.Play();
                    found = true;
                }
            }
        }
    }
    catch
    { }
    if (!found)
        SystemSounds.Beep.Play(); // consolation prize
}
Run Code Online (Sandbox Code Playgroud)

您可以浏览注册表编辑器中的键以查看其他声音。此外,此示例已编码为适用于 Windows 10,我不确定其他版本的 Windows 的注册表结构是什么,因此如果您尝试编码,则需要仔细检查用户使用的操作系统适用于多个平台。