S
Saga
Hi all,
I am now building a routine that reads a small (no larger than 100 bytes)
binary file
and saves the contents in a string. The file can contain characters anywhere
in the
0 - 255 ASCII range. After looking at MSDN Lib. and on the 'net I found a
few
approaches to this problem. I selected one; the resulting code:
Dim strBuf As String = ""
Dim i As Int32
'Check to see if file exists.
If System.IO.File.Exists(strFile) Then
'File exists. Read it.
Dim fs As New System.IO.FileStream(strFile, System.IO.FileMode.Open)
Dim bytAr(fs.Length - 1) As Byte
'Read the file contents.
fs.Read(bytAr, 0, bytAr.Length)
fs.Close()
fs = Nothing
'Build string from byte array.
For i = 0 To bytAr.GetUpperBound(0)
strBuf = strBuf & Chr(bytAr(i))
Next
MessageBox.Show(strBuf)
End if
Is this the best way to do this? It seems that all the methods that I found
always read in the bytes into a byte array. Is there is a way to read the
binary file directly into a string variable? Thanks for the continued
assistance! Saga
I am now building a routine that reads a small (no larger than 100 bytes)
binary file
and saves the contents in a string. The file can contain characters anywhere
in the
0 - 255 ASCII range. After looking at MSDN Lib. and on the 'net I found a
few
approaches to this problem. I selected one; the resulting code:
Dim strBuf As String = ""
Dim i As Int32
'Check to see if file exists.
If System.IO.File.Exists(strFile) Then
'File exists. Read it.
Dim fs As New System.IO.FileStream(strFile, System.IO.FileMode.Open)
Dim bytAr(fs.Length - 1) As Byte
'Read the file contents.
fs.Read(bytAr, 0, bytAr.Length)
fs.Close()
fs = Nothing
'Build string from byte array.
For i = 0 To bytAr.GetUpperBound(0)
strBuf = strBuf & Chr(bytAr(i))
Next
MessageBox.Show(strBuf)
End if
Is this the best way to do this? It seems that all the methods that I found
always read in the bytes into a byte array. Is there is a way to read the
binary file directly into a string variable? Thanks for the continued
assistance! Saga