a_hardin
a_hardin

Reputation: 5260

How can I make the computer beep in C#?

How do I make the computer's internal speaker beep in C# without external speakers?

Upvotes: 130

Views: 153739

Answers (6)

Ta01
Ta01

Reputation: 31630

Use System.Media.SystemSounds to get the sounds for various events, then Play them:

System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Question.Play();
System.Media.SystemSounds.Hand.Play();

Upvotes: 165

Chris Ballance
Chris Ballance

Reputation: 34367

Print the bell character (ASCII code 7). You can use the escape sequence \a from alert/alarm 1.

Console.WriteLine("\a")            

1 \b is for backspace

Upvotes: 16

a_hardin
a_hardin

Reputation: 5260

In .Net 2.0, you can use Console.Beep.

// Default beep
Console.Beep();

You can also specify the frequency and length of the beep in milliseconds.

// Beep at 5000 Hz for 1 second
Console.Beep(5000, 1000);

Upvotes: 222

Barry Kelly
Barry Kelly

Reputation: 42182

The solution would be,

Console.Beep

Upvotes: 23

kuma  DK
kuma DK

Reputation: 1861

It is confirmed that Windows 7 and newer versions (at least 64bit or both) do not use system speaker and instead they route the call to the default sound device.

So, using system.beep() in win7/8/10 will not produce sound using internal system speaker. Instead, you'll get a beep sound from external speakers if they are available.

Upvotes: 10

Jakub Szumiato
Jakub Szumiato

Reputation: 1318

I just came across this question while searching for the solution for myself. You might consider calling the system beep function by running some kernel32 stuff.

using System.Runtime.InteropServices;
        [DllImport("kernel32.dll")]
        public static extern bool Beep(int freq, int duration);

        public static void TestBeeps()
        {
            Beep(1000, 1600); //low frequency, longer sound
            Beep(2000, 400); //high frequency, short sound
        }

This is the same as you would run powershell:

[console]::beep(1000, 1600)
[console]::beep(2000, 400)

Upvotes: 7

Related Questions