-1
请原谅我缺乏知识,这就是为什么我在这里 -从.bin文件读取字节
我想用button2来验证.bin文件的前2个字节。当单击button1时,.bin通过OpenFileDialog1加载到Text1.text中。 我想确保.bin的前2个字节是“ff 4f”,这就是我在十六进制编辑器中打开文件时的情况。 我尝试了几种不同的方法来做到这一点,并没有运气。以下是最近的尝试。随意建议一种新的方式。并感谢您的指导。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim data = New Byte(9) {}
Dim actualRead As Integer
Using fs As New FileStream(Text1.Text, FileMode.Open)
fs.Position = 0
actualRead = 2
Do
actualRead += fs.Read(data, actualRead, 10 - actualRead)
Loop While actualRead <> 10 AndAlso fs.Position < fs.Length
TextBox2.Text = (actualRead)
End Using
End Sub
请注意,如果您的文件超过2GB,这可能会炸毁。但它肯定会做你需要的。另外,我添加了OpenFileDialog代码作为我们正在操作的文件的视觉辅助工具。 – scottyeatscode 2014-09-28 23:57:00
.bin文件是16.5MB,所以我应该是安全的,但是当我应用它时,它似乎只打开另一个OFD提示符。我错过了什么吗? – 2014-09-29 00:18:46
是的,你可以删除OpenFileDialog代码。如果你已经有了文件的路径,那么你需要传递给File.ReadAllBytes()。 File.ReadAllBytes()返回一个包含文件字节内容的字节数组。然后可以使用该字节数组的索引来获取第一个和第二个字节。 – scottyeatscode 2014-09-29 00:28:14