[英]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.