saving a structure array to binary file

ashwin_nath_m

New member
Joined
Oct 13, 2009
Messages
3
Programming Experience
Beginner
Hi all,
Please tell me how to save an array of structure to binary file.
I tried the binary formatter but it still aint workin.
My code attached
Thanks in advance
VB.NET:
    Dim directoryList As String()
    Dim fileData() As myData
    Dim myFile As myProps
    Dim mycount As Integer
    Dim fileArray As String()
    Private Sub SearchDirectory(ByVal currentDirectory As String)
        Try
            Dim fileName As String = ""
            Dim myFile As String
            Dim myDirectory As String
            Dim tmpcount As Integer
            directoryList = Directory.GetDirectories(currentDirectory)
            ' get list of files in current directory
            fileArray = Directory.GetFiles(currentDirectory)
            tmpcount = mycount + fileArray.Length
            ReDim Preserve fileData(tmpcount)
            ' iterate through list of files
            ListView1.BeginUpdate()
            For Each myFile In fileArray
                fileName = myFile.Substring(myFile.LastIndexOf("\") + 1)
                Dim newFileItem As ListViewItem = ListView1.Items.Add(fileName)
                newFileItem.SubItems.Add(myFile.ToString)
                fileData(mycount).fileNames = fileName
                fileData(mycount).filePath = myFile.ToString
                mycount = mycount + 1
            Next
            ListView1.EndUpdate()
            For Each myDirectory In directoryList
                SearchDirectory(myDirectory)
            Next
        Catch unauthorizedAccess As UnauthorizedAccessException


        End Try

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim selPath As String
        mycount = 0
        fd1.ShowDialog()
        myFile = New myProps
        'fileData(0) = New myData
        ListView1.Items.Clear()
        selPath = fd1.SelectedPath
        SearchDirectory(selPath)
        myFile.version = "Ashwin"
        myFile.tCount = mycount
        Dim fs As New FileStream("C:\somefile.dat", FileMode.OpenOrCreate)
        Dim bf As New BinaryFormatter()
        Call bf.Serialize(fs, myFile)
        Call bf.Serialize(fs, fileData)
        fs.Close()
    End Sub
 
When you say it ain't working, what do you mean? Are you getting a file? What is it's contents? Are you getting an error, what error?...


And I'm pretty sure the 'Call' keyword can be removed. I've personally never even heard of it so I'm assuming its just there due to old old versions of VB.

I'm not good with FileMode but wouldn't .Append be appropriate?
 
Back
Top