DllImport isn't really necessary for playing sounds anymore.
If you really need to use DllImport to use the unmanaged sound APIs take a look at this article:
Playing .wav files using C#[
^]. (Note that this article is written with .Net 1.1 and there are much easier ways of playing sound now in the newer versions of the framework)
If you are just playing .wav files and don't need to use windows media player, you could just do:
System.Media.SoundPlayer player = new System.Media.SoundPlayer("SourceFile.wav");
player.Play();
If you want to play other media types or actually want to play sounds through windows media player you first add a reference to C:\Windows\System32\wmp.dll and then use this code:
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayerClass();
wmp.URL = "SoundFile.wav";
wmp.controls.play();
This supports any file type that media player supports.
Finally, you could use the
XNA framework Media namespace[
^]. This supports most of the common formats, but the downside is you'll need to reference the XNA framework libraries:
Uri songFile = new Uri("SoundFile.mp3");
Song yourSong = Song.FromUri("YourSong", songFile );
MediaPlayer.Play(yourSong);