473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

How to check if a sound file is a .wav file in C# ?

Let's say somebody may change the extension of a .doc or .mp3 file to .wav, in my application, I want to check to make sure if a file is actually a .wav file.

How do I do this in C# ?
Mar 5 '10 #1
6 5335
EARNEST
128 100+
Maybe System.IO - > GetExtension?
Or check certain area of the file (header maybe?) where that info can exist.
Mar 5 '10 #2
tlhintoq
3,525 Expert 2GB
Read the file header byte by byte
Mar 5 '10 #3
EARNEST
128 100+
:D very close.
anyways,this might help the OP
Mar 5 '10 #4
GaryTexmo
1,501 Expert 1GB
If you don't care too much about program speeds, you can always just try and play it anyway and use a try/catch block to handle the exception. I don't know what you're using to play the sound, but I did some tests with a System.Media.SoundPlayer object and it seems a System.InvalidOperation exception is thrown when it tries to play a file that's not a valid wav file.

Expand|Select|Wrap|Line Numbers
  1.     public partial class Form1 : Form
  2.     {
  3.         System.Media.SoundPlayer m_player = new System.Media.SoundPlayer();
  4.  
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void button1_Click(object sender, EventArgs e)
  11.         {
  12.             try
  13.             {
  14.                 m_player.SoundLocation = textBox1.Text;
  15.                 m_player.Play();
  16.             }
  17.             catch (System.IO.FileNotFoundException ex)
  18.             {
  19.                 MessageBox.Show("Could not file file: " + textBox1.Text + "\n" + ex.Message);
  20.             }
  21.             catch (System.InvalidOperationException ex)
  22.             {
  23.                 MessageBox.Show("File was not a valid .WAV file: " + textBox1.Text + "\n" + ex.Message);
  24.             }
  25.             catch (Exception ex)
  26.             {
  27.                 MessageBox.Show("Unknown exception: " + ex.Message);
  28.             }
  29.         }
  30.     }
If you're loading and playing a lot of files in quick succession though, the overhead of a try/catch might make it so this method isn't right for you, in which case you'll probably need to go with T's option.
Mar 5 '10 #5
GarryTexmo: if we try to play the sound anyway, it still cannot detect if it's a .wav or, .mp3, .midi...etc.

tlhintoq: "Read the file header byte by byte" => not all files contain a header ?
Mar 5 '10 #6
tlhintoq
3,525 Expert 2GB
Then you just have to try to analyse the file and see if it fits what you *think* it is.

If think it is supposed to be an MP3, then see if the frames fit the specification.

A .wav has a header
http://www.sonicspot.com/guide/wavefiles.html

midi has a header
http://www.skytopia.com/project/articles/midi.html
Mar 6 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
19
by: wetherbean | last post by:
Hi group..I am writing a playlist management protocol where I have a file that holds all the playlists and a file that holds all the songs....before a playlist is created I need to check to see if...
3
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
5
by: jez123456 | last post by:
Hi, I’ve written a c# program to compact certain msaccess databases. The way this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete C:\test1.mdb and rename C:\temp.mdb as...
12
by: Anders Eriksson | last post by:
Hello! I'm trying to create a program that will watch a directory and when a file is created print that file. I have used FileSystemWatcher for watching the directory and I get an created event....
6
by: Dino Buljubasic | last post by:
My application creates some temporary files that are deleted when my application terminates. However, if a temp file is open, it will not be deleted and application will crash. How can I...
9
by: Dino Buljubasic | last post by:
If I want to delete a file I can call File.Delete(filePath) but what happens if I am trying to delete the file that is open??? In java you would do someting like int status =...
3
by: laredotornado | last post by:
Hello, Using PHP 4, how do you check if a file exists on an FTP server given its connection information and a directory to check? Thanks, - Dave
5
by: sword | last post by:
How can I check whether a file exists?
14
by: John Salerno | last post by:
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.