Storing all the data of a binary file into a string

  • Thread starter Thread starter Jason Roozee
  • Start date Start date
J

Jason Roozee

I have submited previous posts about this but still have not come toa
conclustion. I have a crystal report file which is obviously not a text
file, it's a binary file. I want to open the file and store every single
byte of it's data in to a string variable. In my attempts using a
BinaryReader object, I've had no success. I either only get some of the data
or I get a error when PEEKing the data "Index was outside the bounds of the
array".

I have the book "Programming Microsoft Visual Basic .NET" by Microsoft as
my guide to my transision from VB6 to .NET.

In VB6 I would typically use the following code:

Dim strFileData as string
Open fileName for binary access read as #1
strFileData = string$(lof(1), 0)
get #1, , strFileData
Close #1

However, In .NET, it seems impossible. I have attached the file I am trying
to store in to a string so that anyone who would like to try to help me can
have a look at the data I am trying to store. No one seems to be able to
have an answer!

I've tried this in .NET:

Dim sr As StreamReader = File.OpenText(ReportFile.Text)
Do Until sr.Peek = -1
Dim bufferChar(0) As Char
sr.Read(bufferChar, 0, bufferChar.Length)
FileData &= bufferChar(0).ToString
Loop
sr.Close()


But the FileData.Length only ends up being ~68,000 bytes long. Whereas the
file is learly ~104,000 bytes.




Please -- ANYONE! Help me!


Thanks!

Jason Roozee
 
You don't say WHY you want to store it into a string variable. We might
have a better solution.
 
It shouldn't matter why, but ok: I'm trying to take the file's data and
store it in to a string, so I can put it in to my insert statement to put
the data of the file on a SQL server 2000 database (in to a field in a
table) so it can then be pulled from the database, re-saved to a file, and
then the file can be opened from the client end.

Thanks,
Jason
 
Well, the SQL server database has a limit of (I think) 4000 uni-code
characters for any text field. You could use a blob field. Then you are
just reading and writing bytes, which is much simpler than string
conversions.
 
Also, putting it into an "insert" statement is definately the wrong thing to
do. You should have a stored procedure in your database that accepts it as
an "Image" argument (Image is essentially a binary blob). Of course, you
can use parameter fields in your command object to do this as well.
 
Ah-ha. And still.. I have no answer. The data type image and text on SQL
server can store 2,147,483,647 uni-code characters. Not 4000. I wasn't
planning on actually using a insert query, I was only simplifying it for
those whom are not fimular with SQL.


Thanks Robin!

Jason
 
Yes, I assumed you were wanting to use an nvarchar or similar. Blob fields
(Image or Text) are the way to go here I feel.
 
Cool..... So now I still need to get the binary data of the file...........

Jason Roozee
 
Ok, thats the easy part:

Public Function Load ( ByVal filePath As String) As Byte()

Dim theStream As FileStream
Dim theReader As BinaryReader
Dim theData As Byte()

Try
' Open a stream
theStream = New FileStream(filePath, FileMode.Open.Open,
FileAccess.Read)

' Create a reader on the stream
theReader = New BinaryReader(theStream)

' Read in the data
theData = theReader.ReadBytes(CType(theStream.Length, Integer))

Catch ex As Exception

theData = Nothing

Finally

' Close our stream and reader

If Not theStream Is Nothing Then
theStream.Close()
End If

If Not theReader Is Nothing Then
theReader.Close()
End If
End Try

' Return the data

Return theData

End Function
 
Thannnk you...



Robin Tucker said:
Ok, thats the easy part:

Public Function Load ( ByVal filePath As String) As Byte()

Dim theStream As FileStream
Dim theReader As BinaryReader
Dim theData As Byte()

Try
' Open a stream
theStream = New FileStream(filePath, FileMode.Open.Open,
FileAccess.Read)

' Create a reader on the stream
theReader = New BinaryReader(theStream)

' Read in the data
theData = theReader.ReadBytes(CType(theStream.Length, Integer))

Catch ex As Exception

theData = Nothing

Finally

' Close our stream and reader

If Not theStream Is Nothing Then
theStream.Close()
End If

If Not theReader Is Nothing Then
theReader.Close()
End If
End Try

' Return the data

Return theData

End Function

field to
 
you can't store binary data in strings since long time ago
now a string is a unicode string, not ascii string anymore
any attempt to store binary in a string will result in loss of data
 
Back
Top