I am currently doing a project in vb and I want to have an option form where a user can customize the controls (e.g. importing pictures and labels) and I want it to be saved in an external .cfg or another method. P.S. I am still a newbie at this and I will continue to ask more simple questions as I progress on it project.
-
I answered your question, please provide feedback, and mark the answer as accepted/upvote if you find it has answered your question. – AStopher Dec 12 '14 at 09:41
1 Answers
You'll be wanting to write to My.Settings
and saving them with My.Settings.Save
.
First you'll want to define your settings in the project's property pages:
A user
setting creates new settings for each user-account the application is run under, application
setting makes it so that the settings are global and affects everyone regardless of what privileges they hold in the system.
There are also differences to how the settings are saved:
Application: Saves in the [project name].config
file
User: Saves in the <c:\Documents>\<username>\[LocalSettings\]ApplicationData\<companyname>\<appdomainname>_<eid>_<hash>\<version>
You can see more here on where the settings are saved.
Example use-cases:
Application:
- License key (see the 'word of warning' below)
- Administrative settings (proxy settings, filters, etc)
User:
- Username
- Time/Date of last use
- Application colour scheme
In this example I'll use these settings:
If I wanted to set the setting Username
, I would want to do:
My.Settings.Username = "Test"
and to save the settings you would do
My.Settings.Save()
The output file is [project name].config
, and the settings stored in it are similar to this:
<userSettings>
<ConsoleApplication1tst.My.MySettings>
<setting name="PictureLocations" serializeAs="String">
<value />
</setting>
<setting name="Labels" serializeAs="String">
<value />
</setting>
<setting name="Username" serializeAs="String">
<value />
</setting>
<setting name="LastAccessDate" serializeAs="String">
<value />
</setting>
</ConsoleApplication1tst.My.MySettings>
</userSettings>
A word of warning:
Never place passwords or license keys in the application's settings as these can be easily read. If you have to, encrypt them first (and don't use MD5 for it either!).
More information on My.Settings
can be found here.
-
Will the settings be saved in another file or is it in the application itself? – Zeratul1339 Dec 12 '14 at 09:48
-
@Zeratul1339 Updated my answer. I don't actually see the `Username` setting that I set as a test when I built an example program in the config file, so perhaps it's stored in the registry. I'll look further into this. – AStopher Dec 12 '14 at 09:49
-
-
@Zeratul1339 Visual Studio generates the file when the project is compiled, and I *think* the program modifies it. – AStopher Dec 12 '14 at 09:55
-
1That should do it. Keep an eye on my profile for my other future questions. I'm sure you can easily answer those. – Zeratul1339 Dec 12 '14 at 10:03
-
@Zeratul1339 Updated my answer with the locations where the two different Scope types `application` and `user` are saved. – AStopher Dec 12 '14 at 10:04