A small Windows Application to show you how to play a wave (.wav) sound file. I am using the C# IDE from SharpDevelop and the runtime dotnetfx 1.1 from Microsoft, both free downloads. This forms a small and fast student system to write and debug C# programs. From there you can later graduate to the omnipotent, drive space consuming, but more helpful Visual C# .NET.

// play a wave file using PlaySound() from the winmm.dll
// a Windows Application tested with VCS.NET 2003 and SharpDevelop

using System;
using System.Runtime.InteropServices;  // DllImport()
using System.Drawing;
using System.Windows.Forms;

namespace PlayWave1
  // description of MainForm.
  public class MainForm : System.Windows.Forms.Form
    private System.Windows.Forms.Button btnPlay;
    private System.Windows.Forms.Button btnStop;
    private System.Windows.Forms.OpenFileDialog openFileDialog1;
    private System.Windows.Forms.Button btnLoadFile;
    public MainForm()

      openFileDialog1.Title = "Select a Wave Sound File";
      openFileDialog1.Filter = "Wav Files(*.wav)|*.wav";

    public static void Main(string[] args)
      Application.Run(new MainForm());

    #region Windows Forms Designer generated code
    private void InitializeComponent() 
      this.btnLoadFile = new System.Windows.Forms.Button();
      this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
      this.btnStop = new System.Windows.Forms.Button();
      this.btnPlay = new System.Windows.Forms.Button();
      // btnLoadFile
      this.btnLoadFile.Location = new System.Drawing.Point(24, 16);
      this.btnLoadFile.Name = "btnLoadFile";
      this.btnLoadFile.Size = new System.Drawing.Size(120, 23);
      this.btnLoadFile.TabIndex = 0;
      this.btnLoadFile.Text = "Load File";
      this.btnLoadFile.Click += new System.EventHandler(this.BtnLoadFileClick);
      // btnStop
      this.btnStop.Location = new System.Drawing.Point(24, 112);
      this.btnStop.Name = "btnStop";
      this.btnStop.Size = new System.Drawing.Size(128, 23);
      this.btnStop.TabIndex = 2;
      this.btnStop.Text = "Stop";
      this.btnStop.Click += new System.EventHandler(this.BtnStopClick);
      // btnPlay
      this.btnPlay.Location = new System.Drawing.Point(24, 64);
      this.btnPlay.Name = "btnPlay";
      this.btnPlay.Size = new System.Drawing.Size(120, 23);
      this.btnPlay.TabIndex = 1;
      this.btnPlay.Text = "Play";
      this.btnPlay.Click += new System.EventHandler(this.BtnPlayClick);
      // MainForm
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
      this.ClientSize = new System.Drawing.Size(304, 266);
      this.Name = "MainForm";
      this.Text = "MainForm";

    void BtnLoadFileClick(object sender, System.EventArgs e)

    void BtnPlayClick(object sender, System.EventArgs e)
      WSounds ws = new WSounds();
      ws.Play(openFileDialog1.FileName, ws.SND_FILENAME|ws.SND_ASYNC);

    void BtnStopClick(object sender, System.EventArgs e)
      WSounds ws = new WSounds();

  public class WSounds
    public static extern bool  PlaySound(string fname, int Mod, int flag);

    // these are the SoundFlags we are using here, check mmsystem.h for more
    public int SND_ASYNC    = 0x0001;     // play asynchronously
    public int SND_FILENAME = 0x00020000; // use file name
    public int SND_PURGE    = 0x0040;     // purge non-static events
    public void Play(string fname, int SoundFlags)
      PlaySound(fname, 0, SoundFlags);

    public void StopPlay()
      PlaySound(null, 0, SND_PURGE);

12 Years
Discussion Span
Last Post by ashley dane

Approaching C# from C, C++ and Delphi is plain old fun. I like the fact that your code isn't smeared all over your disk in a zillion little files, what you see here is all you need to create this Windows GUI program. The miserable, endless and often missing include files of C and C++ are almost gone. Error messages are a bit wordy, but hey I can actually understand them. I drink to that!


Mental block here!
Anders Hejlsberg, the chief designer for C# was also the chief designer for Borland's Delphi! Correct me if in error! No wonder the many similarities.


Tested it on VC#, which required that the MainForm class be first, so I had to rearrange the order a little.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.