System.Media.SystemSounds plays the sounds the user has assigned to standard Windows events, such as the error “ding” or the default beep. One line does it:
System.Media.SystemSounds.Exclamation.Play();
The class is Windows-only. In a WinForms or WPF project (net8.0-windows with <UseWindowsForms>true</UseWindowsForms> or <UseWPF>true</UseWPF>) it is available out of the box. In a console app it is not part of the base framework; target net8.0-windows and add the System.Windows.Extensions package:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Windows.Extensions" Version="8.0.0" />
</ItemGroup>
All five sounds
using System.Media;
(string Name, SystemSound Sound)[] sounds =
[
("Asterisk", SystemSounds.Asterisk),
("Beep", SystemSounds.Beep),
("Exclamation", SystemSounds.Exclamation),
("Hand", SystemSounds.Hand),
("Question", SystemSounds.Question)
];
foreach (var (name, sound) in sounds)
{
Console.WriteLine(name);
sound.Play();
Thread.Sleep(1500);
}
Play returns immediately and the sound plays in the background. The Thread.Sleep keeps the sounds from overlapping, and in a console app it also keeps the process alive long enough to hear them.
Each property maps to an event in the Sound control panel:
| Property | Sound control panel event |
|---|---|
Asterisk | Asterisk (information) |
Beep | Default Beep |
Exclamation | Exclamation (warning) |
Hand | Critical Stop (error) |
Question | Question |
Why you hear nothing
The sounds are whatever the user configured, which includes nothing at all. On current Windows versions the default scheme leaves Question without a sound, so SystemSounds.Question.Play() is silent on most machines. Users can also pick the “No Sounds” scheme, mute system sounds in the volume mixer, or turn them off on a server. Don’t use a sound as the only signal that something happened.
That is also the reason to prefer SystemSounds over a hard-coded WAV file for alerts: the user’s choices are respected.
You may not need it
MessageBox.Show with an icon already plays the matching sound, so this is enough to get the error sound with an error message:
MessageBox.Show(this, "The file could not be saved.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
For a short beep without any Windows dependency, Console.Beep() works on every platform. The overload that takes a frequency and duration, Console.Beep(800, 200), is Windows-only.
To play your own audio file instead of a system sound, use SoundPlayer for WAV files, also in System.Media, or a library such as NAudio for MP3. The MP3 and WAV playback example covers both.