Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 758
  • Last Modified:

Read a binary file with vb.net

I wil convert an access procedure in vb.net.  This procedure "Create_StkBobis" read the file StkBobix.txt and fill a access table.  Therefore I use a structure type "RecordStkBobis".  Which is the equivalent statement in vb.net ?
Type RecordStkBobis
    S_CodeProduit As String * 5
    S_VL As String * 3
    S_LibelléProduit As String * 42
    S_CodeFacturation As String * 4
    S_Cubage As String * 11
    S_PoidsBrut As String * 11
    S_PoidsNet As String * 11
    S_ColisPalette As String * 8
    S_ArticlesColis As String * 8
    S_DlcMin As String * 4
    S_DlcMax  As String * 4
    S_Palettes  As String * 11
    S_Colis  As String * 11
    S_Articles As String * 11
End Type
Function Create_StkBobis()
    Dim iNumRecord As Double
    Dim MyRecord As RecordStkBobis
    Dim MyDb As Database, MyRecordSet As Recordset
    Set MyDb = DBEngine.Workspaces(0).Databases(0)
    Set MyRecordSet = MyDb.OpenRecordset("StkBobis", dbOpenTable)
    Open "StkBobis.txt" For Random As #1 Len = Len(MyRecord)
    iNumRecord = 0
    While Not EOF(1)
        iNumRecord = iNumRecord + 1
        Get #1, iNumRecord, MyRecord
        MyRecordSet("S_RéfArticle") = iNumRecord - 1
        MyRecordSet("S_CodeProduit") = Left$(MyRecord.S_CodeProduit, 4)
        MyRecordSet("S_VL") = Left$(MyRecord.S_VL, 2)
        MyRecordSet("S_LibelléProduit") = MyRecord.S_LibelléProduit
        MyRecordSet("S_CodeFacturation") = MyRecord.S_CodeFacturation
        MyRecordSet("S_Cubage") = Val(MyRecord.S_Cubage)
        MyRecordSet("S_PoidsBrut") = Val(MyRecord.S_PoidsBrut)
        MyRecordSet("S_PoidsNet") = Val(MyRecord.S_PoidsNet)
        MyRecordSet("S_NbreColisPalette") = Val(MyRecord.S_ColisPalette)
        MyRecordSet("S_NbreArticlesColis") = Val(MyRecord.S_ArticlesColis)
        MyRecordSet("S_DlcMin") = IIf(IsNull(MyRecord.S_DlcMin), 0, Val(MyRecord.S_DlcMin))
        MyRecordSet("S_DlcMax") = IIf(IsNull(MyRecord.S_DlcMax), 0, Val(MyRecord.S_DlcMax))
        MyRecordSet("S_StockPalettes") = Val(MyRecord.S_Palettes)
        MyRecordSet("S_StockColis") = Val(MyRecord.S_Colis)
        MyRecordSet("S_StockArticles") = Val(MyRecord.S_Articles)
    Close #1
End Function

Open in new window

  • 2
  • 2
4 Solutions
Public Function GetFile(ByVal filename As String) As Byte()
    Dim binReader As New BinaryReader(File.Open(Server.MapPath(filename), FileMode.Open, FileAccess.Read))
    binReader.BaseStream.Position = 0
    Dim binFile As Byte() = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length))
    Return binFile
End Function
Imports System.IO

                Dim binReader As New BinaryReader(New StreamReader(FileName).BaseStream)
                'Dim binReader As New BinaryReader(File.Open((FileName), FileMode.Open, FileAccess.Read))
                binReader.BaseStream.Position = 0
                Dim binFile As Byte() = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length))

                Return Convert.ToBase64String(binFile)
Mike TomlinsonMiddle School Assistant TeacherCommented:
It is ~possible~ to write .Net code that works in the same fashion as reading/writing a UDT to a random access file just like in VB6:
(follow the links at the bottom for other pages that discuss this topic)
Phve_364Author Commented:
I have found an other solution which is exactly what I searched>

Structure RecordStkBobis
        <VBFixedString(5)> Public S_CodeProduit As String
        <VBFixedString(3)> Public S_VL As String
        <VBFixedString(42)> Public S_LibelléProduit As String
        <VBFixedString(4)> Public S_CodeFacturation As String
        <VBFixedString(11)> Public S_Cubage As String
        <VBFixedString(11)> Public S_PoidsBrut As String
        <VBFixedString(11)> Public S_PoidsNet As String
        <VBFixedString(8)> Public S_ColisPalette As String
        <VBFixedString(8)> Public S_ArticlesColis As String
        <VBFixedString(4)> Public S_DlcMin As String
        <VBFixedString(4)> Public S_DlcMax As String
        <VBFixedString(11)> Public S_Palettes As String
        <VBFixedString(11)> Public S_Colis As String
        <VBFixedString(11)> Public S_Articles As String
    End Structure
Sub Create_StkBobis()
        Dim myRecord as RecordStkBobis
        Dim RecNum As Integer
        Dim myNumRecord As Integer
        Dim MyDb As dao.Database
        Dim MyRecordSet As dao.Recordset
        MyDb = DAODBEngine_definst.OpenDatabase("c:\test.mdb")
        MyRecordSet = MyDb.OpenRecordset("StkBobis")
       FileOpen(1, myFolderName & "\Stkbobis.fic", OpenMode.Random, OpenAccess.Read, OpenShare.Shared, Len(myRecord))
       myNumRecord = 0
       While Not EOF(1)
            myNumRecord = myNumRecord + 1
            FileGet(1, myRecord, myNumRecord)
            MyRecordSet("S_RéfArticle").Value = myNumRecord - 1
            MyRecordSet("S_CodeProduit").Value = Left$(myRecord1.S_CodeProduit, 4)
            MyRecordSet("S_VL").Value = Left$(myRecord1.S_VL, 2)
            MyRecordSet("S_LibelléProduit").Value = myRecord1.S_LibelléProduit
            MyRecordSet("S_CodeFacturation").Value = myRecord1.S_CodeFacturation
            MyRecordSet("S_Cubage").Value = Val(myRecord1.S_Cubage)
            MyRecordSet("S_PoidsBrut").Value = Val(myRecord1.S_PoidsBrut)
            MyRecordSet("S_PoidsNet").Value = Val(myRecord1.S_PoidsNet)
            MyRecordSet("S_NbreColisPalette").Value = Val(myRecord1.S_ColisPalette)
            MyRecordSet("S_NbreArticlesColis").Value = Val(myRecord1.S_ArticlesColis)
            MyRecordSet("S_DlcMin").Value = IIf(IsNull(MyRecord.S_DlcMin), 0, Val(MyRecord.S_DlcMin))
            MyRecordSet("S_DlcMax").Value = IIf(IsNull(MyRecord.S_DlcMax), 0, Val(MyRecord.S_DlcMax))
            MyRecordSet("S_StockPalettes").Value = Val(myRecord1.S_Palettes)
            MyRecordSet("S_StockColis").Value = Val(myRecord1.S_Colis)
            MyRecordSet("S_StockArticles").Value = Val(myRecord1.S_Articles)
        End While
    End Sub

Open in new window

Mike TomlinsonMiddle School Assistant TeacherCommented:
Right...that is also in the series I linked to in my previous post.
Here is the first article in the series: http://msdn.microsoft.com/en-us/library/aa903289(VS.71).aspx

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now