Saving Structure To file

  • Thread starter Thread starter Lespaul36
  • Start date Start date
L

Lespaul36

I have an icon structure..so it is a structure with other structures in it.
I need to save it to a binary file. I have done some googling..but not much
luck.

How can I do this? Also, will long data type be converted to the proper 4
bytes? Do I need to go through each structure and make the stream that way?
If so, how do I convert longs into the 4 byte arrays?
TIA
 
I have an icon structure..so it is a structure with other structures in it.
I need to save it to a binary file. I have done some googling..but not much
luck.

How can I do this? Also, will long data type be converted to the proper 4
bytes? Do I need to go through each structure and make the stream that way?
If so, how do I convert longs into the 4 byte arrays?
TIA

Here's a simple example... (by the way, a long in VB.NET is 8 bytes or
64-bit)

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Module Module1

<Serializable()> _
Structure inner
Public i As Long
Public j As Integer
End Structure

<Serializable()> _
Structure outer
Public st As inner
Public v As String
End Structure

Sub Main()
Dim s As outer

s.st.i = Long.MaxValue
s.st.j = Integer.MinValue
s.v = "My Name"

' write it out...
Dim fs As FileStream = File.Open("structure.bin",
FileMode.OpenOrCreate)
Dim fmtr As New BinaryFormatter
fmtr.Serialize(fs, s)
fs.Close()

' read it in...
fs = File.Open("structure.bin", FileMode.Open)
Dim s1 As outer = DirectCast(fmtr.Deserialize(fs), outer)
fs.Close()

Console.WriteLine(s1.v)
Console.WriteLine(s1.st.i)
Console.WriteLine(s1.st.j)

Console.ReadLine()
End Sub

End Module
 
I am sorry, I didn't phrase myself correctly.
I need to convert all info in the structure to a binary file, not to save
the info in the structure to rebuild the structure, but because the icon
file is made with the all the info in the structure. Using serialize will
save the structure object...I need to make it in to a readable file for an
icon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top