问题
Using the code:
using (var reader = new WaveFileReader(audioFileLocation))
{
// Do something....
}
If given a wav file that throws the exception:
Not a WAVE file - no RIFF header
Exception Details: System.FormatException: Not a WAVE file - no RIFF header
It locks the file audioFileLocation
which prevents it from being deleted.
Is there any way to check for the existence of a valid RIFF header before using the reader?
回答1:
Try using a stream:
using(var inputStream = new FileStream(audioFileLocation, FileMode.Open,
FileAccess.Write, FileShare.ReadWrite))
{
using (var reader = new WaveFileReader(inputStream))
{
// Do something....
}
}
If THIS is the current code of the WaveFileReader
class it uses File.OpenRead(waveFile)
in the "string overload" of the consturctor and the Stream
returned seems not to be closed/disposed. Maybe already the follownin works:
using(var inputStream = File.OpenRead(audioFileLocation))
{
using (var reader = new WaveFileReader(inputStream))
{
// Do something....
}
}
as this should dispose the stream.
来源:https://stackoverflow.com/questions/24824027/not-a-wave-file-no-riff-header-locks-file