Hi all, i have a problem reading from a file in vb.net. I want to read from a file bit by bit so the file i write to is exactly the same, well almost because im planning on merging two files together but i can do that if i can figure out how to read and write exactly as the file was. I cant seem to get streamreader to work on non text files.

Im probably just being retarded, but its been a long day so any help would be greatly appreciated.

Many thanks crackruckles

Comments
Don't post if you don't won't to respond.

convert the file into a binary stream and read as you want to.

I don't think you can merge binary files.

ok i have managed to knock up some code based on stuff i have found on the internet but the only problem i have now is i cant get the last three bytes of the file to copy across, here is the code so far:

Public Sub read(ByVal file1 As String, ByVal file3 As String)
        Dim writeStream As FileStream
        Dim pos As Integer = 0
        writeStream = New FileStream(file3, FileMode.Create)
        Dim writeBinay As New BinaryWriter(writeStream)
        Try
            Using reader As New BinaryReader(File.Open(file1, FileMode.Open))
                Dim length As Integer = reader.BaseStream.Length
                While pos < length - 4
                    Dim value As Integer = reader.ReadInt32()
                    writeBinay.Write(value)
                    pos += 4
                End While
            End Using
        Catch fileException As FileNotFoundException
            label1.text = ("File Does Not Exits")
        Catch serializableException As SerializationException
            label1.text = ("Error Writing to File")
        Catch formattingException As FormatException
            label1.text = ("Invalid Format")
        Catch e As IOException
            label1.text = ("Cannot close file")
        End Try
        writeBinay.Close()
    End Sub

Merge binary file

'Easy Way
My.Computer.FileSystem.WriteAllBytes("FileToMerge", My.Computer.FileSystem.ReadAllBytes("File1"), True)
My.Computer.FileSystem.WriteAllBytes("FileToMerge", My.Computer.FileSystem.ReadAllBytes("File2"), True)

'Other Way
Dim MergedFileStream As New IO.FileStream("FileToMerge", IO.FileMode.Create)
Dim MergedBinaryWriter As New IO.BinaryWriter(MergedFileStream)

Dim ExistingFileStream As IO.FileStream
Dim ExistingFileReader As IO.BinaryReader

ExistingFileStream = New IO.FileStream("File1", IO.FileMode.Open)
ExistingFileReader = New IO.BinaryReader(ExistingFileStream)

MergedBinaryWriter.Write(ExistingFileReader.ReadBytes(ExistingFileStream.Length))

ExistingFileStream.Dispose()
ExistingFileReader.Close()

ExistingFileStream = New IO.FileStream("File2", IO.FileMode.Open)
ExistingFileReader = New IO.BinaryReader(ExistingFileStream)

MergedBinaryWriter.Write(ExistingFileReader.ReadBytes(ExistingFileStream.Length))

ExistingFileStream.Dispose()
ExistingFileReader.Close()

MergedBinaryWriter.Close()
MergedFileStream.Dispose()
This article has been dead for over six months. Start a new discussion instead.