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.AddNew
        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)
        MyRecordSet.Update
    Wend
    Close #1
    MyRecordSet.Close
End Function

Open in new window

STKBOBIS.txt
Phve_364Asked:
Who is Participating?
 
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.AddNew()
            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)
            MyRecordSet.Update()
        End While
        FileClose(1)
        MyRecordSet.Close()
        MyDb.Close()
    End Sub

Open in new window

0
 
RamanaChoudaryCommented:
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))
    binReader.Close()
    Return binFile
End Function
0
 
RamanaChoudaryCommented:
or
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))
                binReader.Close()

                Return Convert.ToBase64String(binFile)
0
 
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconOpeningFilesForRandomAccess.asp
(follow the links at the bottom for other pages that discuss this topic)
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.