[RESOLVED] Save and load variable values from/to file
Hello.
I've been literally googling for an hour now trying to find how to save variable settings to a file. Now i turn to you guys on this forum for help.
I'm writing an application and I'm using alot of changeable variables. I want to be able to save these to a file so they can be loaded at application startup. I don't feel like using the regestry since I hate to write unimportant things to the registry.
I remembered that I found a good example with simple code of how to do this with xml, but I can't seem to find the example now.
Could someone please explain to me how to do this in an easy and smart way.
Thank you :)
Re: Save and load variable values from/to file
You certainly shouldn't be using the Registry because the .NET Framework introduced application config files specifically so you wouldn't. VB 2008 introduced My.Settings to make it even easier. Open the project properties and select the Settings page. Now click the link at the top and read on for the solution to your problem.
Re: Save and load variable values from/to file
Re: Save and load variable values from/to file
Ok so I got it working alright.
BUT, something is messed up. The values does change and is saved if I restart the application.
The <applicationName>.exe.config stays the same. It doesn't change. Where the hell are the settings stored? :-o What's happened? :-o
And yes I have built the application so I don't run it in debugger.
I'm using this code to modify:
Code:
With My.Settings
.Test = textbox1.text
End With
Re: Save and load variable values from/to file
Quote:
Originally Posted by
jmcilhinney
vb 2008 introduced my.settings to make it even easier.
vb 2005..?
Re: Save and load variable values from/to file
@noob scripter
I think it's just My.Settings.Test = TextBox1.Text. Not sure what your with statement is for.
Re: Save and load variable values from/to file
Well I believe that 'with' is a way to cut down on the codewriting. I guess there something more it but for example:
If you have long variablenames and would end up with:
VB Code:
UserHandler.GetUser.First.User.FirstName="Stefan"
UserHandler.GetUser.First.User.LastName="Karlsson"
UserHandler.GetUser.First.User.Age="39"
UserHandler.GetUser.First.User.Sex="Male"
UserHandler.GetUser.First.User.Occupation="Programmer"
UserHandler.GetUser.First.User.UserID="0"
then I would use WITH to make it more readable:
VB Code:
With UserHandler.GetUser.First.User
.FirstName="Stefan"
.LastName="Karlsson"
.Age="39"
.Sex="Male"
.Occupation="Programmer"
.UserID="0"
end with
And in this application I do have a lot of lines :p
Re: Save and load variable values from/to file
Quote:
Originally Posted by
n00b scripter
Ok so I got it working alright.
BUT, something is messed up. The values does change and is saved if I restart the application.
The <applicationName>.exe.config stays the same. It doesn't change. Where the hell are the settings stored? :-o What's happened? :-o
And yes I have built the application so I don't run it in debugger.
I'm using this code to modify:
Code:
With My.Settings
.Test = textbox1.text
End With
The application config file contains the initial values for all your settings. Any settings you create with User scope will then be copied into a user config file stored under each user's Documents and Settings folder. When you edit settings at run time, that's where the changes are made. This also means that there is a different set of settings values for each Windows user who uses your application.
Re: Save and load variable values from/to file
Quote:
Originally Posted by
jmcilhinney
The application config file contains the initial values for all your settings. Any settings you create with User scope will then be copied into a user config file stored under each user's Documents and Settings folder. When you edit settings at run time, that's where the changes are made. This also means that there is a different set of settings values for each Windows user who uses your application.
Aah, I see. Okey I found where it's stored. I'm using the VS on my school computer and the values are store in a folder named as the companys name which is my schools. This folder is automatically created and named the same on every computer. Is there anyway to change this in the application? I don't really feel that it would be good if a publish it on the internet and the folder is called as that companys name on some unknown users computer.
Re: Save and load variable values from/to file
If you want a different company name then change the company name. Click the assembly Information button on the Application tab of the project properties.
Re: Save and load variable values from/to file
Quote:
Originally Posted by
jmcilhinney
If you want a different company name then change the company name. Click the assembly Information button on the Application tab of the project properties.
Ok thank you :) I think I got it all working now. Thx for all the great help