Playing Windows System Sounds in .NET 2.0
User feedback can often be enhanced with the use of audio. Microsoft Windows provides several sounds to indicate errors, questions and other information with the sounds configured to the user's preference. These can be played using .NET 2.0 classes.
The .NET Framework 2.0 defines a namespace named System.Media that contains a set of classes used for playing sounds. This namespace includes the SystemSounds class, which holds five static properties to represent key sounds that are user-configurable through the Control Panel. Using these properties, we can play the sounds that the user has allocated to the events using C#, Visual Basic or any other .NET language. If the user has elected not to receive an audible feedback for an event, no sound is played but no exception is thrown so processing continues as expected.
NB: These classes are available from .NET framework 2.0 onwards. Users of earlier versions of the .NET framework cannot apply this method to play system sounds.
The Five System Sounds
The five system sound properties are all SystemSound objects and therefore include a Play method. When executed, this method causes the sound to be played. The properties are named as follows:
Using the properties described above, a system sound can be played using the following code. To change the sound played, simply select another property. Remember that the SystemSounds class is in the System.Media namespace so add using System.Media; to the start of your code file.
1 November 2007