level42
level42

Reputation: 987

Getting HEX data at specific offset for multiple files (VB.net)

I have working code that will examine a file and pull the correct hex data at a given offset. I have modified this code to work with "OpenFileDialog's" multiselect.

However, my code is stuck reading the first file, over and over again, thus each file has the same HEX output.

I need this to parse through each file discover in the OpenFileDialog.

Currently, what ever the first file in the list "Multiselected" is the only file that is read, over and over. None of the other files get examined.

My code is below:

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

Can someone please help me figure out why?

I've tried adding the "Files" string variable here:

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

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

But that simply renders:

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

Additional information: StartIndex cannot be less than zero.

Upvotes: 0

Views: 344

Answers (1)

LarsTech
LarsTech

Reputation: 81620

SafeFileNames does not include the path, so your BinaryReader can't find the file (it's probably looking in the bin\debug folder).

Try changing it to:

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

because the FileNames collection does contain the path. If you still want to use the SafeFileNames, then you will have to use Path.Combine and put the folder path back into your file name yourself.

Upvotes: 3

Related Questions