Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#反对的已处理异常-我如何更正它们?_C#_Objectdisposedexception - Fatal编程技术网

c#反对的已处理异常-我如何更正它们?

c#反对的已处理异常-我如何更正它们?,c#,objectdisposedexception,C#,Objectdisposedexception,我是初学者。 我想让程序从.wav文件转换成.raw文件。 我找到了一些来源,我想使用它。 但是代码中发生了一些事情。该错误与ObjectDisposedException有关 你能给我一些代码或想法吗? 整个代码正在修改中 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace

我是初学者。 我想让程序从.wav文件转换成.raw文件。 我找到了一些来源,我想使用它。 但是代码中发生了一些事情。该错误与ObjectDisposedException有关

你能给我一些代码或想法吗? 整个代码正在修改中

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;

    namespace WaveTestRead
    {
       class WaveReader
       { 
    FileInfo m_fInfo;
    FileStream m_fStream;
    BinaryReader m_binReader;

    // RIFF chunk
    byte[] chunkID;
    UInt32 chunkSize;
    byte[] format;

    // fmt subchunk
    byte[] fmtChunkID;
    UInt32 fmtChunkSize;
    UInt16 audioFormat;
    UInt16 numChannels;
    UInt32 sampleRate;
    UInt32 byteRate;
    UInt16 blockAssign;
    UInt16 BitsPerSample;

    // data subchunk
    byte[] dataChunkID;
    UInt32 dataChunkSize;
    byte[] data8L;              // 8-bit left channel
    byte[] data8R;              // 8-bit right channel
    Int16[] data16L;           // 16-bit left channel
    Int16[] data16R;           // 16-bit right channel
    int numSamples;

    public WaveReader()
    {

    }

    public bool Open(String filename)
    {
        string str;
        m_fInfo = new FileInfo(filename);
        m_fStream = m_fInfo.OpenRead();
        m_binReader = new BinaryReader(m_fStream);

        chunkID = new byte[4];
        format = new byte[4];

        chunkID = m_binReader.ReadBytes(4);
        chunkSize = m_binReader.ReadUInt32();
        format = m_binReader.ReadBytes(4);

        str = System.Text.ASCIIEncoding.ASCII.GetString(chunkID, 0, 4);
        if (str != "RIFF")
            return false;

        str = System.Text.ASCIIEncoding.ASCII.GetString(format, 0, 4);
        if (str != "WAVE")
            return false;

        if (ReadFmt() == false)
            return false;
        if (ReadData() == false)
            return false;

        m_fStream.Close();

        return true;
    }

    private bool ReadFmt()
    {
        fmtChunkID = new byte[4];
        fmtChunkID = m_binReader.ReadBytes(4);

        string str = System.Text.ASCIIEncoding.ASCII.GetString(fmtChunkID, 0, 4);
        if (str != "fmt ")
            return false;

        fmtChunkSize = m_binReader.ReadUInt32();
        audioFormat = m_binReader.ReadUInt16();
        numChannels = m_binReader.ReadUInt16();
        sampleRate = m_binReader.ReadUInt32();
        byteRate = m_binReader.ReadUInt32();
        blockAssign = m_binReader.ReadUInt16();
        BitsPerSample = m_binReader.ReadUInt16();

        return true;
    }
   static void Main(string[] args)
    {
        p.Open("wavetest.wav");
        bool a = p.ReadFmt();
        p.ReadData();
    }
 }
 }

问题可能存在于
Main
中。
Open
方法在末尾关闭文件,而
ReadFmt
调用Main从
m\u binReader
读取。在调用
Open
之后,不要从main方法调用
ReadFmt
,或者将
Open
更改为在文件完成时不关闭文件(并明确关闭)


看起来
Open
正在为您完成所有工作(通过调用
ReadFmt
ReadData
)。您无需在
Main
中再次执行此操作;只需访问
p

中的数据发布的代码写得不是很好

无论如何,您可以尝试以下快速更改:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WaveTestRead
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (var waveReader = new WaveReader())
            {
                if (!waveReader.Open("wavetest.wav"))
                {
                    Console.WriteLine("Failed to read file.");
                    return;
                }

                if (!waveReader.ReadFmt())
                {
                    Console.WriteLine("Failed to read fmt.");
                    return;
                }

                // this method is not defined...
                //waveReader.ReadData();
            }

        }
    }

    class WaveReader : IDisposable
    {
        FileInfo m_fInfo;
        FileStream m_fStream;
        BinaryReader m_binReader;

        // RIFF chunk
        byte[] chunkID;
        UInt32 chunkSize;
        byte[] format;

        // fmt subchunk
        byte[] fmtChunkID;
        UInt32 fmtChunkSize;
        UInt16 audioFormat;
        UInt16 numChannels;
        UInt32 sampleRate;
        UInt32 byteRate;
        UInt16 blockAssign;
        UInt16 BitsPerSample;

        // data subchunk
        byte[] dataChunkID;
        UInt32 dataChunkSize;
        byte[] data8L;              // 8-bit left channel
        byte[] data8R;              // 8-bit right channel
        Int16[] data16L;           // 16-bit left channel
        Int16[] data16R;           // 16-bit right channel
        int numSamples;

        public WaveReader()
        {

        }

        public bool Open(String filename)
        {
            string str;
            m_fInfo = new FileInfo(filename);
            m_fStream = m_fInfo.OpenRead();
            m_binReader = new BinaryReader(m_fStream);

            chunkID = new byte[4];
            format = new byte[4];

            chunkID = m_binReader.ReadBytes(4);
            chunkSize = m_binReader.ReadUInt32();
            format = m_binReader.ReadBytes(4);

            str = System.Text.ASCIIEncoding.ASCII.GetString(chunkID, 0, 4);
            if (str != "RIFF")
                return false;

            str = System.Text.ASCIIEncoding.ASCII.GetString(format, 0, 4);
            if (str != "WAVE")
                return false;

            //if (ReadFmt() == false)
            //    return false;
            //if (ReadData() == false)
            //    return false;

            return true;
        }

        public bool ReadFmt()
        {
            fmtChunkID = new byte[4];
            fmtChunkID = m_binReader.ReadBytes(4);

            string str = System.Text.ASCIIEncoding.ASCII.GetString(fmtChunkID, 0, 4);
            if (str != "fmt ")
                return false;

            fmtChunkSize = m_binReader.ReadUInt32();
            audioFormat = m_binReader.ReadUInt16();
            numChannels = m_binReader.ReadUInt16();
            sampleRate = m_binReader.ReadUInt32();
            byteRate = m_binReader.ReadUInt32();
            blockAssign = m_binReader.ReadUInt16();
            BitsPerSample = m_binReader.ReadUInt16();

            return true;
        }

        public void Dispose()
        {
            if (m_fStream != null)
                m_fStream.Dispose();
        }
    }
}

基本上,我用您的代码创建了一个WaveReader类,并删除了对ReadFmt的内部调用。然后在Main方法中,我检查了返回代码,如果为false,我将写入控制台。

Open()函数不应调用m_fStream.Close()。这不是一个好代码。@Hans Passant你能给我更精确的解释吗?我在这里发现错误fmtChunkID=m_binReader.ReadBytes(4);我检查了代码“boola=p.ReadFmt();”使函数读取两次。我认为第一个功能调用是可以的,但第二个是不可取的。如何使函数只读取一次?是的,这失败了,因为m_binReader使用m_fStream。已经关闭了。@HansPassant那么我应该删除这个m_fStream.close();密码?或者我可以用另一种方式来处理吗?