How to add basic sounds to a .NET console application
September 16, 2015 2 Comments
We all know a couple of very annoying features of web sites: popups, flashing text, rotating images and the like. The good news is that there are some equally annoying features that you can add to .NET console applications: constantly changing titles, console window size and position, text size and colour etc.
In this short post we’ll see how to add a basic beep to a console app. As with the above mentioned features you should not overuse it.
The Console object has a Beep method which has a parameterless version:
Console.Beep();
It plays a short beep sound. If you’re not satisfied with it you can go for the overloaded Beep method that accepts a frequency in hertz and a duration in milliseconds. The lower the frequency value the deeper the sound:
Console.Beep(100, 5000);
That sounds like an organ.
Console.Beep(10000, 5000);
That on the other hand just sounds awful.
UPDATE: Saeid mentioned another way to produce sounds in console apps below in the comments section: the SystemSounds class.
View all various C# language feature related posts here.
Also it’s possible to call
SystemSounds.Hand.Play();
SystemSounds.Hand.Play();
I didn’t know that, thanks for the tip. I’ll extend the post. //Andras