Results 1 to 2 of 2

Thread: Easyest way to approach saving structures?

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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:
    1. This class opens a file with the name passed into the constructor. It then
    2. 'either writes or reads one item to it for each write/read item call.
    3. 'This Class assumes the file will be located in the AppPath() folder.
    4. Public Class FileDataStorePDA
    5.     Private fileName As String
    6.     Private bisValid As Boolean
    7.     Private readValid As Boolean
    8.     Private sWriter As System.IO.StreamWriter
    9.     Private sReader As System.IO.StreamReader
    10.     Private clearFlag As Boolean
    11.  
    12. #Region "Constructors and Destructors"
    13.     Public Sub New(ByVal st1 As String)
    14.         st1 = AppPath() & "\" & st1
    15.  
    16.         Try
    17.             sWriter = System.IO.File.AppendText(st1)
    18.             bisValid = True
    19.             fileName = st1
    20.             clearFlag = False
    21.         Catch ex As Exception
    22.             bisValid = False
    23.             clearFlag = True
    24.         End Try
    25.  
    26.     End Sub
    27.  
    28.     Public Sub Dispose()
    29.         Try
    30.             sReader.Close()
    31.         Catch ex As Exception
    32.             'Nothing needs to be done here.
    33.         End Try
    34.  
    35.         Try
    36.             sWriter.Close()
    37.         Catch ex As Exception
    38.             'Nothing needs to be done here, either.
    39.         End Try
    40.     End Sub
    41. #End Region
    42.  
    43. #Region "Public"
    44.     Public Function WriteItem(ByVal st1 As String) As Boolean
    45.         If bisValid Then
    46.             If clearFlag Then
    47.                 clearFlag = False
    48.                 sWriter.Close()
    49.                 System.IO.File.Delete(fileName)
    50.                 sWriter = System.IO.File.AppendText(fileName)
    51.             End If
    52.             sWriter.WriteLine(st1)
    53.             WriteItem = True
    54.         Else
    55.             If readValid Then
    56.                 Try
    57.                     sReader.Close()
    58.                     If clearFlag Then
    59.                         clearFlag = False
    60.                         System.IO.File.Delete(fileName)
    61.                     End If
    62.                     sWriter = System.IO.File.AppendText(fileName)
    63.                     sWriter.WriteLine(st1)
    64.                     WriteItem = True
    65.                 Catch ex As Exception
    66.                     'Don't know if I really want this to do anything.
    67.                     WriteItem = False
    68.                 Finally
    69.                     readValid = False
    70.                     bisValid = True
    71.                 End Try
    72.             Else
    73.                 WriteItem = False
    74.             End If
    75.         End If
    76.     End Function
    77.  
    78.     Public Function ClearFile()
    79.         clearFlag = True
    80.     End Function
    81.  
    82.     Public Function ReadItem() As String
    83.         If readValid Then
    84.             Try
    85.                 ReadItem = sReader.ReadLine
    86.                 If ReadItem Is Nothing Then
    87.                     ReadItem = ""
    88.                 End If
    89.             Catch ex As Exception
    90.                 MsgBox("A problem occurred with reading the file:" & vbNewLine & ex.Message, MsgBoxStyle.Information, "Problem")
    91.                 ReadItem = ""
    92.             End Try
    93.         Else
    94.             If bisValid Then
    95.                 Try
    96.                     sWriter.Close()
    97.                     sReader = New System.IO.StreamReader(fileName, System.Text.Encoding.ASCII)
    98.                     ReadItem = sReader.ReadLine
    99.                     readValid = True
    100.                 Catch ex As Exception
    101.                     MsgBox("Can't read item:" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, "Problem Reading")
    102.                     ReadItem = ""
    103.                     readValid = False
    104.                 Finally
    105.                     'Invalidate the reading, anyways.
    106.                     readValid = True
    107.                     bisValid = False
    108.                 End Try
    109.             End If
    110.         End If
    111.     End Function
    112.  
    113.     Public Function CanRead() As Boolean
    114.         CanRead = readValid
    115.     End Function
    116.  
    117.     Public Function CanWrite() As Boolean
    118.         CanWrite = bisValid
    119.     End Function
    120.  
    121. #End Region
    122.  
    123. #Region "Private"
    124.     'The function that gets the path of the application.
    125.     Private Function AppPath()
    126.         AppPath = System.IO.Path.GetDirectoryName(Reflection.Assembly.GetCallingAssembly.GetName.CodeBase.ToString)
    127.     End Function
    128. #End Region
    129.  
    130. End Class
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width