Results 1 to 8 of 8

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

  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.

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

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

    First of all, thanks for giving me credit also.

    Now about why I posted:
    In the SaveToFile sub, you have DeleteFile wich is not a VB keyword (maybe your using API for that ?), well I think it should be "Kill FileName"

    Second problem:
    It gives error when I copy & paste this code:
    VB Code:
    1. Sub Form_Unload()
    2.     Settings.SaveToFile App.Path & "\Settings.bin"
    3. End Sub
    4.  
    5. ' it should be:
    6. [b]Private Sub Form_Unload(Cancel As Integer)[/b]
    7.     Settings.SaveToFile App.Path & "\Settings.bin"
    8. End Sub

    And a note to add to the original idea:
    One nice thing about this method of saving your settings is that you can easily encrypt all the data in the PropertyBag before you save the data, and decrypt when you load the data.
    (Instead of encrypting each value one by one)

    [Edit]
    I also use this method (using the PropertyBag.Contents Property) to easily send settings/data over a winsock connection. Makes things a little easier.
    Last edited by CVMichael; Jan 30th, 2006 at 07:10 PM.

  3. #3
    New Member
    Join Date
    Jun 2006
    Posts
    3

    Exclamation Re: VB - Save/Load settings to/from file

    this is exactly what i'm looking for, i have to save an option that is either on or off, but i want my program to retrieve that option when it is closed and re-opened, but i have no idea how i would utilize the code you have provided

    hope you can help

  4. #4
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

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

    Why not just save the settings to the registry as long as the users have permission to do so.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  5. #5
    New Member
    Join Date
    Jun 2006
    Posts
    3

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

    thats a good idea, how to i read and right from the registry lol

  6. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

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

    Quote Originally Posted by nicodemusashes
    thats a good idea, how to i read and right from the registry lol
    Use SaveSetting and GetSetting

  7. #7
    New Member
    Join Date
    Jun 2006
    Posts
    3

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

    sorry, but i've never worked with registry settings before, how exactly would i do that?

  8. #8
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

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

    Quote Originally Posted by nicodemusashes
    sorry, but i've never worked with registry settings before, how exactly would i do that?
    It's not rocket science.... really....

    Just type "SaveSetting" in VB, then press F1

    Or use Google, this is the first link on Google that I found by searching for SaveSetting, and it happes to be right in our own forum !
    http://www.vbforums.com/showthread.php?t=232113

    If you don't know something, do a search, it's much faster than waiting for us to respond on the forums, right ?
    And if you can't find anything by searching, then post the question, otherwise we will search for your question, and post the links like I just did... what's the point of that ?

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