Results 1 to 6 of 6

Thread: save settings

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    save settings

    i have a settings window in my app where the user can change some properties like background color...
    what is the best way to save these so they get loaded the next time i start the app? there are about 50 settings, most are boolean or integers. nothing complicated?
    should i use the registry, or create a settings.txt file or ...?
    any recommendations as to what is most professional? thanks...

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    I know this has been discussed around here before - so maybe search around to find various ways to do it.

    Personally I like creating a properties class. This contains all the stuff I want to persist. Then I use XML serialization and deserialization on the properties object.

    Works just fine, easy to do and easy to edit the xml file.

  3. #3

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    XML serialization and deserialization
    what is that?
    can you explain what you're talking about or send me to somewhere where i can learn?

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Sure, but you may want to explore other options as well. I just happen to like this way, it doesn't mean there aren't better ways to do it.

    Serialization is a big subect, in this case we're persisting an object to a file, then deserializing the file back into an object.

    Here's my properties class:
    VB Code:
    1. Public Class SleuthNCICServiceProperties
    2.     ' The server's address.
    3.     Public serverAddress As String
    4.  
    5.     ' The server's port.
    6.     Public serverPort As Integer
    7.  
    8.     ' The listening port.
    9.     Public listenPort As Integer
    10.  
    11.     ' Lots of decisions in the server application
    12.     ' are based upon the stateUS. i.e. NM, CO, etc.
    13.     Public stateUS As String
    14.  
    15.     Public sendInitialRegisterString As Boolean
    16.     Public initialRegisterORI As String
    17.  
    18.     Public sendHeartbeat As Boolean
    19.     Public heartbeatInterval As Integer
    20.     Public heartbeatCharacter As Integer
    21. End Class

    This is how I dim my object in a different class:
    VB Code:
    1. Public Class SleuthNCICService
    2.     Inherits System.ServiceProcess.ServiceBase
    3.  
    4.     ' Configuration properties.
    5.     Public Shared properties As New SleuthNCICServiceProperties
    6. .
    7. .
    8. .

    I do this at the start of the application. It's an example of serialization in both directions:

    VB Code:
    1. Private Sub ReadProperties()
    2.         Dim fileName As String = Application.StartupPath & "\Sleuth-NCIC-Service-properties.xml"
    3.         Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(SleuthNCICServiceProperties))
    4.  
    5.         'A first time thing only - if the properties file does not
    6.         'exist, create a default one.
    7.         If Not File.Exists(fileName) Then
    8.             properties.serverAddress = "localhost"
    9.             properties.serverPort = 4243
    10.             properties.listenPort = 4242
    11.             properties.stateUS = "NM"
    12.             properties.sendInitialRegisterString = True
    13.             properties.initialRegisterORI = "NM1234567"
    14.             properties.sendHeartbeat = True
    15.             properties.heartbeatInterval = 60000
    16.             properties.heartbeatCharacter = 5
    17.  
    18.             Try
    19.                 Dim myWriter As StreamWriter = New StreamWriter(fileName)
    20.                 mySerializer.Serialize(myWriter, properties)
    21.                 myWriter.Close()
    22.             Catch ex As Exception
    23.                 Logger.Log("E", Encoding.ASCII.GetBytes(ex.ToString))
    24.             End Try
    25.         End If
    26.  
    27.         'Deserialize the xml file into a properties object
    28.         Try
    29.             Dim myFileStream As FileStream = New FileStream(fileName, FileMode.Open)
    30.             properties = CType(mySerializer.Deserialize(myFileStream), SleuthNCICServiceProperties)
    31.             myFileStream.Close()
    32.         Catch ex As Exception
    33.             Logger.Log("E", Encoding.ASCII.GetBytes(ex.ToString))
    34.         End Try
    35.     End Sub ' ReadProperties()

    The xml file looks like this:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <SleuthNCICServiceProperties xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <serverAddress>127.0.0.1</serverAddress>
      <serverPort>4243</serverPort>
      <listenPort>4242</listenPort>
      <stateUS>NM</stateUS>
      <sendInitialRegisterString>true</sendInitialRegisterString>
      <initialRegisterORI>NM0260701</initialRegisterORI>
      <sendHeartbeat>true</sendHeartbeat>
      <heartbeatInterval>60000</heartbeatInterval>
      <heartbeatCharacter>5</heartbeatCharacter>
    </SleuthNCICServiceProperties>
    Then in your code, you use the class members:

    VB Code:
    1. ReadProperties()
    2.         If properties.sendHeartbeat Then
    3.             TimerKeepAlive.Interval = properties.heartbeatInterval
    4.             TimerKeepAlive.Enabled = True
    5.         End If

  5. #5

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    thanks a lot. i'll digest and try all of this tomorrow. i'm sure i'll have some more questions then.

    thanks again.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

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