简体   繁体   English

类型为“ System.FormatException”的未处理异常

[英]An unhandled exception of type 'System.FormatException'

I'm having a simple method inside my button. 我的按钮内有一个简单的方法。 The logic behind is simple but i cant understand why I'm getting this error on the String set. 背后的逻辑很简单,但我不明白为什么我在String集合上收到此错误。

Here is the error 这是错误

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll中发生了'System.FormatException'类型的未处理异常

Additional information: Too many bytes in what should have been a 7 bit encoded Int32. 附加信息:本来应该是7位编码的Int32的字节太多。

Here is the code 这是代码

   private void button1_Click(object sender, EventArgs e)
    {
        FileStream data= new FileStream("data.DAT", FileMode.OpenOrCreate);
        BinaryReader a = new BinaryReader(data);
        while (data.Position < data.Length)
        {
            string name = a.ReadString();
            string egn = a.ReadString();
            string lenght = a.ReadString();
            int hour = a.ReadInt32();
            double price = a.ReadDouble();
            int days = a.ReadInt32();
            double result = price* hour* days;
            dataGridView1.Rows.Add(name, egn, lenght, hour, price, days, result);
        }
        data.Close();
    }

Without seeing the exact data it is hard to say, but I suspect you are misusing BinaryReader. 没有看到确切的数据很难说,但是我怀疑您正在滥用BinaryReader。 For example, the ReadString method assumes the string to read is prefixed with the length in a specific format. 例如,ReadString方法假定要读取的字符串以特定格式的长度作为前缀。

https://msdn.microsoft.com/en-us/library/system.io.binaryreader.readstring(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.io.binaryreader.readstring(v=vs.110).aspx

Basically, BinaryReader is designed to read from a file produced by BinaryWriter, not some arbitrary binary file. 基本上,BinaryReader旨在读取BinaryWriter生成的文件,而不是某些任意的二进制文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发生类型为&#39;System.FormatException&#39;的未处理的异常 - An unhandled exception of type 'System.FormatException' occurred 未处理的异常:System.FormatException - Unhandled Exception: System.FormatException mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll 类型为“ System.FormatException”的未处理的异常输入字符串的格式不正确 - Unhandled exception of type “System.FormatException” Input string was not in correct format 退出应用程序后,类型为&#39;System.FormatException&#39;的未处理异常 - An unhandled exception of type 'System.FormatException' after exiting the app “ mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理的异常” - “An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll” mscorlib.dll中发生了未处理的“System.FormatException”类型异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll System.Speech.dll 中发生类型为“System.FormatException”的未处理异常 - An unhandled exception of type 'System.FormatException' occurred in System.Speech.dll System.Net.Http.dll中发生了未处理的“System.FormatException”类型异常 - An unhandled exception of type 'System.FormatException' occurred in System.Net.Http.dll mscorlib.dll中发生了&#39;System.FormatException&#39;类型的未处理的异常其他信息:输入字符串的格式不正确 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM