![](/img/trans.png)
[英]An unhandled exception of type 'System.FormatException' occurred
[英]An unhandled exception of type 'System.FormatException'
我的按钮内有一个简单的方法。 背后的逻辑很简单,但我不明白为什么我在String集合上收到此错误。
这是错误
mscorlib.dll中发生了'System.FormatException'类型的未处理异常
附加信息:本来应该是7位编码的Int32的字节太多。
这是代码
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();
}
没有看到确切的数据很难说,但是我怀疑您正在滥用BinaryReader。 例如,ReadString方法假定要读取的字符串以特定格式的长度作为前缀。
https://msdn.microsoft.com/en-us/library/system.io.binaryreader.readstring(v=vs.110).aspx
基本上,BinaryReader旨在读取BinaryWriter生成的文件,而不是某些任意的二进制文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.