problem deserialization serialization

A

andreas

Hi,

if have a object arrSdList of type SortedList for serialization and
deserialization
there are two subs for doing this

Public Sub deser
Dim Formatter As BinaryFormatter = New BinaryFormatter()
Dim ReadFile As FileStream
ReadFile = File.OpenRead(FilePad)
arrSdList = CType(Formatter.Deserialize(ReadFile), SortedList)
ReadFile.Close()
End Sub

Public Sub ser
Dim Formatter As BinaryFormatter = New BinaryFormatter()
Dim SaveFile As FileStream
SaveFile = File.OpenWrite(FilePad)
SaveFile.Seek(0, SeekOrigin.End)
Formatter.Serialize(SaveFile, arrSdList)
SaveFile.Close()
End Sub

when i do
ser
deser
there is no exception message

but when i do
ser
deser
ser
i get the exception message for the second ser :

Binary stream does not contain a valid BinaryHeader, 0 possible causes,
invalid stream or object version change between serialization and
deserialization.

what is wrong with the subs ?
who can help me ?
 
A

andreas

Info for the question
As value for the arrSdList i have a class instance defined as

<serializable> public class clsPerson
 
G

Greg Burns

Here is the code I am using. It looks roughly the same as yours. Haven't
actually tried calling ser, deser, ser again, so it may behave the same.
Give it a shot.

HTH,
Greg


Public Shared Function LoadSettings() As UserSettings

Dim mySettings As New UserSettings

Dim s As String =
Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)

s &= "\PCO\pco.bin"

If File.Exists(s) Then

Dim streamOut As FileStream

Try
Dim formatter As New BinaryFormatter

streamOut = New FileStream(s, FileMode.Open,
FileAccess.Read)

mySettings = CType(formatter.Deserialize(streamOut),
UserSettings)

Catch

Finally
streamOut.Close()
End Try
End If

Return mySettings

End Function

Public Shared Sub SaveSettings(ByVal mySettings As UserSettings)
Dim s As String =
Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData)

s &= "\PCO"

If Not Directory.Exists(s) Then

Directory.CreateDirectory(s)

End If

s &= "\pco.bin"

Dim formatter As New BinaryFormatter

Dim streamIn As New FileStream(s, FileMode.Create,
FileAccess.Write)

formatter.Serialize(streamIn, mySettings)

streamIn.Close()

End Sub
 

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

Top