Beep Sound in C#.NET Framework

This gets asked all the time on our internal alias, so I am sure google gets asked a lot, here is my effort to provide an answer.

No, the .NET Framework V1.0\V1.1 does not support Beep()… In those platforms you can

(a) PInvoke to Win32:

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

(b) See MessageBeepType

 

(c) Use the Beep sound in the VB assembly
Add a reference to the Microsoft.VisualBasic.dll assembly and use

     Microsoft.VisualBasic.Interaction.Beep()

Of course in Whidbey (.NET Framework V2.0) we support Beep() directly in the Framework:

Via Console.Beep() and Console.Beep(int frequency, in duration).

 

Update (11/22/05):

For the .NET Compact Framework I'd suggest you use:

namespace CFBeep
{
public partial class Form1 : Form
{
[DllImport("coredll.dll")]
public static extern int PlaySound(
string szSound,
IntPtr hModule,
int flags);

        public Form1()
{
InitializeComponent();
}

        private void button1_Click(object sender, EventArgs e)
{
PlaySound(@"\Windows\Voicbeep", IntPtr.Zero, (int)(PlaySoundFlags.SND_FILENAME | PlaySoundFlags.SND_SYNC));

        }
}

    public enum PlaySoundFlags : int
{
SND_SYNC = 0x0, // play synchronously (default)
SND_ASYNC = 0x1, // play asynchronously
SND_NODEFAULT = 0x2, // silence (!default) if sound not found
SND_MEMORY = 0x4, // pszSound points to a memory file
SND_LOOP = 0x8, // loop the sound until next sndPlaySound
SND_NOSTOP = 0x10, // don't stop any currently playing sound
SND_NOWAIT = 0x2000, // don't wait if the driver is busy
SND_ALIAS = 0x10000, // name is a registry alias
SND_ALIAS_ID = 0x110000,// alias is a predefined ID
SND_FILENAME = 0x20000, // name is file name
SND_RESOURCE = 0x40004, // name is resource name or atom
}
}