Use BinaryReader and BinaryWriter in vb.net

I was I trying to use the dos copy command to append MPEG-2 PS files together but found all I needed was the BinaryReader and BinaryWriter. Here is what I came up with. Hope it helps.

Imports System.IO
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MergeFiles("C:\test.mp3", "C:\Temp\Annie Lennox - A Whiter Shade Of Pale.mp3", "C:\Temp\Annie Lennox - Bare - 05 - Wonderful.mp3")
        MsgBox("done ")
    End Sub
Private Sub MergeFiles(ByVal OutFile As String, ByVal ParamArray Files() As String)
        File.Copy(Files(0), OutFile, True) 'overwrites if exists.
        Const ChunkSize As Integer = 1024 * 1024
        Dim bw As BinaryWriter = Nothing
        Try
            bw = New BinaryWriter(New FileStream(OutFile, FileMode.Append))
            For i As Integer = 1 To Files.GetUpperBound(0)
                Dim br As BinaryReader = Nothing
                Try
                    br = New BinaryReader(New FileStream(Files(i), FileMode.Open))
                    Do While br.BaseStream.Position < br.BaseStream.Length - 1
                        Dim b(ChunkSize - 1) As Byte
                        Dim ReadLen As Integer = br.Read(b, 0, ChunkSize)
                        bw.Write(b, 0, ReadLen)
                    Loop
                Catch ex As Exception
                    MsgBox(ex.Message)
                Finally
                    If Not br Is Nothing Then
                        If Not br.BaseStream Is Nothing Then br.BaseStream.Dispose()
                        br.Close()
                    End If
                End Try
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If Not bw Is Nothing Then
                If Not bw.BaseStream Is Nothing Then bw.BaseStream.Dispose()
                bw.Close()
            End If
        End Try
    End Sub

End Class

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: