In the uncompressed situation I know I need to read the wav header, pull out the number of channels, bits, and sample rate and work it out from there: (channels) * (bits) * (samples/s) * (seconds) = (filesize)
Is there a simpler way - a free library, or something in the .net framework perhaps?
How would I do this if the .wav file is compressed (with the mpeg codec for example)?
Download NAudio.dll from the link http://naudio.codeplex.com/
and then use this function
you will get the Duration
I had difficulties with the example of the MediaPlayer-class above. It could take some time, before the player has opened the file. In the "real world" you have to register for the MediaOpened-event, after that has fired, the NaturalDuration is valid. In a console-app you just have to wait a few seconds after the open.
There's a bit of a tutorial (with - presumably - working code you can leverage) over at CodeProject.
The only thing you have to be a little careful of is that it's perfectly "normal" for a WAV file to be composed of multiple chunks - so you have to scoot over the entire file to ensure that all chunks are accounted for.
Try code below from How to determine the length of a .wav file in C#
Download "PresentationCore.dll" and "WindowsBase.dll" from:
http://www.search-dll.com/dll-files/download/windowsbase.dll.html
Paste the files in you application bin folder for reference. It should work now.
In the .net framework there is a mediaplayer class:
http://msdn.microsoft.com/en-us/library/system.windows.media.mediaplayer_members.aspx
Here is an example:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2667714&SiteID=1&pageid=0#2685871