Determining FileSize

ok I use FileOpen to open a file, and the files that I need to open are of a specific size, and any bigger/smaller and it will cause the game to crash. It is for Populous, the header files.

Here is my code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myFile As String = ""

Dim Dr As DialogResult = OpenFileDlg.ShowDialog

If Dr = Windows.Forms.DialogResult.OK Then

myFile = OpenFileDlg.FileName

'hfile = New String(Chr(0), 616)

hfile = New String(Chr(0), 616)

FileOpen(1, myFile, OpenMode.Binary)

FileGet(1, hfile)

If hfile.Length <> 616 Then

FileClose(1)

MsgBox("Invalid File! Size mismatch!")

Me.Text = "Header Viewer"

Exit Sub

Else

tb1.Text = CStr(Asc(Mid(hfile, 90, 1)))

tb2.Text = CStr(Asc(Mid(hfile, 91, 1)))

tb3.Text = CStr(Asc(Mid(hfile, 92, 1)))

FileClose(1)

firstclick = True

Me.Text = "Header Viewer: " & myFile

End If

ElseIf Dr = Windows.Forms.DialogResult.Cancel Then

Exit Sub

End If

End Sub

It works ok if the length is less than 616 bytes/characters long, but any longer and it simply chops it off and fits in what it can, what Can I use to determine filesize before transferring it into the string



Answer this question

Determining FileSize

  • Taya Cool

    Use the FileInfo class instead....

    Dim fi As New IO.FileInfo("ThePath")

    Dim MyFileSize As Long = fi.Length



  • Mike Herrick

    Thanks, I shall try that.
  • Determining FileSize