我如何使用BinaryReader获取错误?

时间:2012-08-16 01:55:20

标签: c#

我需要读取二进制文件。但是得到错误。我该怎么做 ? 我试着解释我还能写些什么吗?

using System;
using System.IO;
using System.Net;
using System.Text;

namespace BinaryReader
{
    public partial class Form1 : Form1
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void R()
        {
            using (BinaryReader br = new BinaryReader(File.Open("file.bin", FileMode.Open)))
            {
                // 2.
                // Position and length variables.
                int pos = 0;
                // 2A.
                // Use BaseStream.
                int length = (int)b.BaseStream.Length;
                while (pos < length)
                {
                    // 3.
                    // Read integer.
                    int v = b.ReadInt32();
                    Console.WriteLine(v);

                    // 4.
                    // Advance our position variable.
                    pos += sizeof(int);
                }
            }
        }
    }


}

在这一行我得到错误:

using (BinaryReader br = new BinaryReader(File.Open("file.bin", FileMode.Open)))

错误'BinaryReader'是'命名空间',但用作'type'

我该如何解决?

1 个答案:

答案 0 :(得分:3)

您与自己的命名空间发生了名称冲突。例如,将其重命名为BinaryReaderTest,或使用System.IO.BinaryReader System.IOBinaryReader的全名:

using (var br = new System.IO.BinaryReader(File.Open("file.bin", FileMode.Open)))