Easyest way to approach saving structures?
I need to save a structure. Unfortunately, BinaryFormatters and Serializors don't exist in the compact frame work.
What would be the easyest way to save and then load to and from a file?
I don't know Xml and all the Xml serialization examples I have seen seem overly complicated.
Re: Easyest way to approach saving structures?
Frankly, I think a text file would be the easiest. There is no security at all there (though you could encrypt the info), but streamwriters/readers are supported by the CF. If you write each element on a single line, then you can read it back in in the same order. Encapsulating the reading and writing in a single class would make it fairly easy to work with.
Here's a class I wrote to do something like that. It was written in haste, and may not be all that efficient, but it works.
VB Code:
This class opens a file with the name passed into the constructor. It then
'either writes or reads one item to it for each write/read item call.
'This Class assumes the file will be located in the AppPath() folder.
Public Class FileDataStorePDA
Private fileName As String
Private bisValid As Boolean
Private readValid As Boolean
Private sWriter As System.IO.StreamWriter
Private sReader As System.IO.StreamReader
Private clearFlag As Boolean
#Region "Constructors and Destructors"
Public Sub New(ByVal st1 As String)
st1 = AppPath() & "\" & st1
Try
sWriter = System.IO.File.AppendText(st1)
bisValid = True
fileName = st1
clearFlag = False
Catch ex As Exception
bisValid = False
clearFlag = True
End Try
End Sub
Public Sub Dispose()
Try
sReader.Close()
Catch ex As Exception
'Nothing needs to be done here.
End Try
Try
sWriter.Close()
Catch ex As Exception
'Nothing needs to be done here, either.
End Try
End Sub
#End Region
#Region "Public"
Public Function WriteItem(ByVal st1 As String) As Boolean
If bisValid Then
If clearFlag Then
clearFlag = False
sWriter.Close()
System.IO.File.Delete(fileName)
sWriter = System.IO.File.AppendText(fileName)
End If
sWriter.WriteLine(st1)
WriteItem = True
Else
If readValid Then
Try
sReader.Close()
If clearFlag Then
clearFlag = False
System.IO.File.Delete(fileName)
End If
sWriter = System.IO.File.AppendText(fileName)
sWriter.WriteLine(st1)
WriteItem = True
Catch ex As Exception
'Don't know if I really want this to do anything.
WriteItem = False
Finally
readValid = False
bisValid = True
End Try
Else
WriteItem = False
End If
End If
End Function
Public Function ClearFile()
clearFlag = True
End Function
Public Function ReadItem() As String
If readValid Then
Try
ReadItem = sReader.ReadLine
If ReadItem Is Nothing Then
ReadItem = ""
End If
Catch ex As Exception
MsgBox("A problem occurred with reading the file:" & vbNewLine & ex.Message, MsgBoxStyle.Information, "Problem")
ReadItem = ""
End Try
Else
If bisValid Then
Try
sWriter.Close()
sReader = New System.IO.StreamReader(fileName, System.Text.Encoding.ASCII)
ReadItem = sReader.ReadLine
readValid = True
Catch ex As Exception
MsgBox("Can't read item:" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, "Problem Reading")
ReadItem = ""
readValid = False
Finally
'Invalidate the reading, anyways.
readValid = True
bisValid = False
End Try
End If
End If
End Function
Public Function CanRead() As Boolean
CanRead = readValid
End Function
Public Function CanWrite() As Boolean
CanWrite = bisValid
End Function
#End Region
#Region "Private"
'The function that gets the path of the application.
Private Function AppPath()
AppPath = System.IO.Path.GetDirectoryName(Reflection.Assembly.GetCallingAssembly.GetName.CodeBase.ToString)
End Function
#End Region
End Class