|
-
Jan 30th, 2006, 12:54 AM
#1
Thread Starter
Hyperactive Member
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:
Option Explicit
' CSettings 1/27/2006
Private Keys() As String
Private Vals() As String
Private Settings As Integer
Private Sub Class_Initialize()
Settings = -1
End Sub
Private Function IsSet(ByVal Key As String) As Integer
Dim i As Integer
If Settings <> -1 Then
For i = 0 To Settings
If Keys(i) = Key Then
IsSet = i
Exit Function
End If
Next
End If
IsSet = -1
End Function
Public Sub Save(ByVal Key As String, ByVal Val As String)
Dim i As Integer
i = IsSet(Key)
If i = -1 Then
Settings = Settings + 1
ReDim Preserve Keys(Settings)
ReDim Preserve Vals(Settings)
Keys(Settings) = Key
Vals(Settings) = Val
Else
Vals(i) = Val
End If
End Sub
Public Function Load(ByVal Key As String) As String
Dim i As Integer
i = IsSet(Key)
If i = -1 Then
Load = ""
Else
Load = Vals(i)
End If
End Function
Public Sub SaveToFile(ByVal Filename As String)
Dim PB As New PropertyBag
Dim i As Integer
Dim Binary() As Byte
If Len(Dir(Filename)) <> 0 Then
DeleteFile Filename
End If
PB.WriteProperty "s", Settings
For i = 0 To Settings
PB.WriteProperty "k" & i, Keys(i)
PB.WriteProperty "v" & i, Vals(i)
Next
i = FreeFile
Open Filename For Binary As i
Binary = PB.Contents
Put i, , Binary
Erase Binary
Close i
End Sub
Public Sub LoadFromFile(ByVal Filename As String)
Dim PB As New PropertyBag
Dim i As Integer
Dim Binary() As Byte
i = FreeFile
Open Filename For Binary As i
ReDim Binary(LOF(i))
Get i, , Binary
PB.Contents = Binary
Erase Binary
Close i
Erase Keys
Erase Vals
Settings = Int(PB.ReadProperty("s"))
For i = 0 To Settings
ReDim Preserve Keys(i)
ReDim Preserve Vals(i)
Keys(i) = PB.ReadProperty("k" & i)
Vals(i) = PB.ReadProperty("v" & i)
Next
End Sub
Example usage:
VB Code:
Dim Settings As New CSettings
Sub Form_Load()
If Len(Dir(App.Path & "\Settings.bin")) <> 0 Then
Settings.LoadFromFile App.Path & "\Settings.bin"
Else
Settings.Save "Started", 0
End If
Settings.Save "Started", Settings.Load("Started") + 1
End Sub
Sub Form_Unload()
Settings.SaveToFile App.Path & "\Settings.bin"
End Sub
Last edited by frozen; Feb 3rd, 2007 at 07:46 AM.
-
Jan 30th, 2006, 06:58 PM
#2
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:
Sub Form_Unload()
Settings.SaveToFile App.Path & "\Settings.bin"
End Sub
' it should be:
[b]Private Sub Form_Unload(Cancel As Integer)[/b]
Settings.SaveToFile App.Path & "\Settings.bin"
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.
-
Jun 19th, 2006, 12:57 PM
#3
New Member
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
-
Jun 20th, 2006, 06:55 AM
#4
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."
-
Jun 20th, 2006, 12:54 PM
#5
New Member
Re: VB - Save/Load settings to/from file
thats a good idea, how to i read and right from the registry lol
-
Jun 20th, 2006, 01:04 PM
#6
Re: VB - Save/Load settings to/from file
 Originally Posted by nicodemusashes
thats a good idea, how to i read and right from the registry lol 
Use SaveSetting and GetSetting
-
Jun 20th, 2006, 01:15 PM
#7
New Member
Re: VB - Save/Load settings to/from file
sorry, but i've never worked with registry settings before, how exactly would i do that?
-
Jun 20th, 2006, 02:08 PM
#8
Re: VB - Save/Load settings to/from file
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|