How to beep with .NET?

Posted: (EET/GMT+2)

 

When Windows or a Windows application has something to tell you, you will often see a simple message box on the screen, along with a traditional sound: a beep, pling, boing, or what have you.

A simple sound is a nice attention grabber, until the user gets tired of them and mutes the speakers. Of course, you can also play these so-called System Sounds (one can customize these through Control Panel) from your own applications, but surprisingly enough, there hasn't been anything in .NET Framework to help you do this. Instead, you had to resort to calling the Win32 API directly.

Luckily, as .NET 2.0 is here, Microsoft has added a new namespace to the .NET Base Class Library (BCL). This namespace is simply called System.Media, and it is implemented in System.dll, which means that you don't have to add any new references to your project to use this namespace.

This namespace has a nice class called SystemSounds which allows you to play any of the most common Windows system sounds, for example the ubiquitous beep. To make your application beep, you would simply do this in C#:

System.Media.SystemSounds.Beep.Play();

Naturally, you would also need to add a using System.Media; statement to your source file. Happy beepin'! :-)