获取多个文件的特定偏移量的HEX数据(VB.net)

时间:2015-08-05 22:06:33

标签: vb.net hex openfiledialog

我有工作代码,它将检查文件并在给定的偏移量处提取正确的十六进制数据。我已修改此代码以使用“OpenFileDialog”的多选。

但是,我的代码一遍又一遍地读取第一个文件,因此每个文件都有相同的HEX输出。

我需要这个来解析OpenFileDialog中的每个文件发现。

目前,“Multiselected”列表中的第一个文件是唯一一次读取的文件。没有其他文件被检查过。

我的代码如下:

Int_openDiag()

        Dim dr As DialogResult = Me.OpenDialog.ShowDialog()
        If (dr = System.Windows.Forms.DialogResult.OK) Then

            Dim Files As String
            For Each Files In OpenDialog.SafeFileNames
                         Try

                    Using OpenedFile As New BinaryReader(File.Open(OpenDialog.FileName, FileMode.Open))
                        ' Loop through length of file.
                        Dim fileLength As Long = OpenedFile.BaseStream.Length
                        Dim byteCount As Integer = 0
                        Dim pos As Long = "&H" + TextBox1.Text                  'Offset to scan. (Scan starting point)
                        OpenedFile.BaseStream.Seek(pos, SeekOrigin.Begin)
                        While pos < fileLength And byteCount < requiredBytes
                            value(byteCount) = OpenedFile.ReadByte()
                            pos += 1
                            byteCount += 1
                        End While

                        displayValue = BitConverter.ToString(value)

                        Dim newItem As New ListViewItem(Files)
                        newItem.SubItems.Add(displayValue)
                        newItem.SubItems.Add("0x" + TextBox1.Text)
                        ListView1.Items.Add(newItem)

                    End Using
                Catch SecEx As Security.SecurityException

                Catch ex As Exception

                End Try
            Next Files

        End If

有人可以帮我找出原因吗?

我在这里尝试添加“Files”字符串变量:

Using OpenedFile As New BinaryReader(File.Open(Files, FileMode.Open))

----------------------------------------------- ----------------------- ^

但这简单地呈现:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: StartIndex cannot be less than zero.

1 个答案:

答案 0 :(得分:3)

SafeFileNames不包含路径,因此您的BinaryReader无法找到该文件(它可能在bin \ debug文件夹中查找)。

尝试将其更改为:

For Each Files In ofd.FileNames
  Using OpenedFile As New BinaryReader(File.Open(Files, FileMode.Open))

因为FileNames集合确实包含路径。如果您仍想使用SafeFileNames,则必须使用Path.Combine并自行将文件夹路径放回文件名中。