How can I make the computer beep in C#?

早过忘川 提交于 2019-11-26 15:20:02

问题


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


回答1:


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);

For more information refer http://msdn.microsoft.com/en-us/library/8hftfeyw%28v=vs.110%29.aspx




回答2:


You can also use the relatively unused:

    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();

Documentation for this sounds is available in http://msdn.microsoft.com/en-us/library/system.media.systemsounds(v=vs.110).aspx




回答3:


Console.Beep




回答4:


Try this

Console.WriteLine("\a")




回答5:


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

So,Using system.beep() in win7/8/10 will not sound using internal system speaker. But You'll get a beep sound form external speakers if it is available.




回答6:


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)


来源:https://stackoverflow.com/questions/321135/how-can-i-make-the-computer-beep-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!