Onboard Speakersound (Beep) abspielen

// Aufrufbeispiel
// SpeakerBeep.PlayBeep(1000, 1000);

using System.Runtime.InteropServices;

/// <summary->
/// freeware helper class for creating a onboard system speaker beep
/// (W) 2011 by admin of codezentrale.6x.to
/// </summary->
public static class SpeakerBeep
{
    /// <summary->
    /// http://pinvoke.net/default.aspx/kernel32.Beep
    /// </summary->
    [DllImport(&quot;kernel32.dll&quot;, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool Beep(uint dwFreq, uint dwDuration);
    /// <summary->
    /// Plays a beepsound on onboard speaker
    /// </summary->
    /// <param name=&quot;freq&quot;->desired frequency [Hz]</param->
    /// <param name=&quot;duration&quot;->desired duration [ms]</param->
    /// <returns->true, if successfull, otherwise false</returns->
    public static bool PlayBeep(uint freq, uint duration)
    {
        return Beep(freq, duration);
    }
}

Wav-Datei abspielen

// Objekt erzeugen
System.Media.SoundPlayer sp = new System.Media.SoundPlayer();
// WAV Datei als Stream zuweisen
sp.Stream = new FileStream("c:\\Testfile.wav", FileMode.Open, FileAccess.Read);
// Stream abspielen
sp.Play();

...

// anhalten
sp.Stop();
// Objekt zerstören
sp.Dispose();

oder

// Objekt erzeugen
System.Media.SoundPlayer sp = new System.Media.SoundPlayer("c:\\Testfile.wav");
// abspielen
sp.Play();

...

// anhalten
sp.Stop();
// Objekt zerstören
sp.Dispose();

Systemsounds abspielen

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