Results 1 to 8 of 8

Thread: VB - Save/Load settings to/from file

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    294

    VB - Save/Load settings to/from file

    With this class you can Save your programs settings to a file, and load it on startup. It uses a PropertyBag for its saving and loading because if its .Contents property. This class was inspired by CVMichael.

    Add a new class module to your project, name it CSettings and add the following code to it
    VB Code:
    1. Option Explicit
    2.  
    3. ' CSettings 1/27/2006
    4.  
    5. Private Keys() As String
    6. Private Vals() As String
    7. Private Settings As Integer
    8.  
    9. Private Sub Class_Initialize()
    10.     Settings = -1
    11. End Sub
    12.  
    13. Private Function IsSet(ByVal Key As String) As Integer
    14.     Dim i As Integer
    15.     If Settings <> -1 Then
    16.         For i = 0 To Settings
    17.             If Keys(i) = Key Then
    18.                 IsSet = i
    19.                 Exit Function
    20.             End If
    21.         Next
    22.     End If
    23.     IsSet = -1
    24. End Function
    25.  
    26. Public Sub Save(ByVal Key As String, ByVal Val As String)
    27.     Dim i As Integer
    28.     i = IsSet(Key)
    29.     If i = -1 Then
    30.         Settings = Settings + 1
    31.         ReDim Preserve Keys(Settings)
    32.         ReDim Preserve Vals(Settings)
    33.         Keys(Settings) = Key
    34.         Vals(Settings) = Val
    35.     Else
    36.         Vals(i) = Val
    37.     End If
    38. End Sub
    39.  
    40. Public Function Load(ByVal Key As String) As String
    41.     Dim i As Integer
    42.     i = IsSet(Key)
    43.     If i = -1 Then
    44.         Load = ""
    45.     Else
    46.         Load = Vals(i)
    47.     End If
    48. End Function
    49.  
    50. Public Sub SaveToFile(ByVal Filename As String)
    51.     Dim PB As New PropertyBag
    52.     Dim i As Integer
    53.     Dim Binary() As Byte
    54.     If Len(Dir(Filename)) <> 0 Then
    55.         DeleteFile Filename
    56.     End If
    57.     PB.WriteProperty "s", Settings
    58.     For i = 0 To Settings
    59.         PB.WriteProperty "k" & i, Keys(i)
    60.         PB.WriteProperty "v" & i, Vals(i)
    61.     Next
    62.     i = FreeFile
    63.     Open Filename For Binary As i
    64.         Binary = PB.Contents
    65.         Put i, , Binary
    66.         Erase Binary
    67.     Close i
    68. End Sub
    69.  
    70. Public Sub LoadFromFile(ByVal Filename As String)
    71.     Dim PB As New PropertyBag
    72.     Dim i As Integer
    73.     Dim Binary() As Byte
    74.     i = FreeFile
    75.     Open Filename For Binary As i
    76.         ReDim Binary(LOF(i))
    77.         Get i, , Binary
    78.         PB.Contents = Binary
    79.         Erase Binary
    80.     Close i
    81.     Erase Keys
    82.     Erase Vals
    83.     Settings = Int(PB.ReadProperty("s"))
    84.     For i = 0 To Settings
    85.         ReDim Preserve Keys(i)
    86.         ReDim Preserve Vals(i)
    87.         Keys(i) = PB.ReadProperty("k" & i)
    88.         Vals(i) = PB.ReadProperty("v" & i)
    89.     Next
    90. End Sub
    Example usage:
    VB Code:
    1. Dim Settings As New CSettings
    2. Sub Form_Load()
    3.     If Len(Dir(App.Path & "\Settings.bin")) <> 0 Then
    4.         Settings.LoadFromFile App.Path & "\Settings.bin"
    5.     Else
    6.         Settings.Save "Started", 0
    7.     End If
    8.     Settings.Save "Started", Settings.Load("Started") + 1
    9. End Sub
    10.  
    11. Sub Form_Unload()
    12.     Settings.SaveToFile App.Path & "\Settings.bin"
    13. End Sub
    Last edited by frozen; Feb 3rd, 2007 at 07:46 AM.

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