private void button2_Click(object sender, EventArgs e)
        {
            string fileName = "MyNew.data";
            if (!(File.Exists(fileName)))
            {
                MessageBox.Show("当前文件不存在");
                return;
            }
            string strData = "";
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(fs);
            strData = reader.ReadString();
            for (int i = 0; i < 200;i++ )
            {
                if (i == 0)
                {
                    strData += reader.ReadUInt32().ToString();                }
                else
                {
                    strData += "||" + reader.ReadUInt32().ToString();
                }
                textBox2.Text = strData;
                fs.Close();
                reader.Close();
            }
        }
标红部分报的错“无访问已关闭的文件”,但是前面fs已经是打开模式了啊?谢谢各位大侠。谢谢

解决方案 »

  1.   


    fs.Close();
    reader.Close();
    移到循环外啊,否则循环一次就关闭了。
      

  2.   

    不是在i==0的时候抱错的,而是在else中,说明第一次连接,第二次被你关闭了。
      

  3.   

    我在关闭文件后又定义了一个新的FileStream对象来读取文件,为什么还是提示“无法访问关闭的文件”?
    namespace BinaryFileTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string filepath = @"D:\Test13.001";
                if(!File.Exists(filepath))
                {
                    Console.WriteLine("file not exist!");
                    return;
                }
                FileStream fstream=new FileStream (filepath ,FileMode.Open,FileAccess.Read);
                BinaryReader breader=new BinaryReader (fstream);
                Console.WriteLine("filelength" + fstream.Length.ToString()+"bytes");
                string outstr;//用于输出的字符串
                outstr=breader.ReadInt16().ToString ();
                Console.WriteLine("[1] HEDER VERSION: "+outstr);
                fstream.Close();
                breader.Close();            //重新定义
                FileStream fstream2 = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                BinaryReader breader2 = new BinaryReader(fstream2,Encoding.Default);
                breader2.BaseStream.Position = 58;
                char[] cc = new char[8];
                for (int i = 0; i < 8; i++)
                    cc[i] = breader.ReadChar();
                Console.WriteLine(cc);
                fstream2.Close();
                breader2.Close();
             }
        }
    }
    谁能帮我分析分析??
      

  4.   

    解决了, breader.Close();//在前面
            fstream.Close();