`Not a WAVE file - no RIFF header` locks file

对着背影说爱祢 提交于 2019-12-11 04:51:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!