How to Read, Write, Modify & Remove Application Settings From Config File Using VB 2005?
.NET Framework 1.1 provides System.Configuration Namespace which exposes methods for reading information from the config file, but there are no methods for modifying the contents.
Is there anyway this can be done using Visual Basic 2005, without implementing a custom class?
[455 byte] By [
JobLot] at [2007-12-16]
Hi,
Good news! There are very simple features to do this in VB 2005: the Project Designer and My.Settings.
First, open the Project Designer by double-clicking My Project, and click on Settings tab. You can add named settings there.
Second, in code you can access these properties by typing in My.Settings.[yoursettingname].
You can read and write to these settings. E.g.
| |
'read a setting name = My.Settings.ProductName'write a setting My.Settings.ProductName = "Visual Basic"
|
Hope this helps.
Paul Yuknewicz
Visual Basic
I have to change the Scope to "User" on Settings tab, otherwise it was getting declared as Public ReadOnly property.
Secondly, on click of a button changed the value of the property, but it was not written back to my config file? What cud have gone wrong?
Correct, only User settings can be modified/saved. Sorry I didn't point that out. User settings are the default in builds post Beta2 (and that's what I'm using now ;) ).
You should be seeing that the application is saving these config values, and reloads the updated settings upon app launch, correct? If that's the case then you're probably looking to see the change in the app.exe.config file rather than the user.config file. Changes will only persist in the user directory .config file, so you need to look there; however in most occaisions you can work through the My.Settings APIs rather than with these files.
Otherwise, if the app is not actually saving the settings at all you might have flipped a switch in the Project Designer that turns off Auto Saving of settings. You can work around this manually by writing the code to My.Settings.Save explicitly at the right time.
| |
Sub ChangeAndPersistSettings() My.Settings.LastChanged = Today My.Settings.Save() End Sub
|
Hopefully one of these fixes will do the trick for you. Let us know.
Thanks,
Paul Yuknewicz
and where would i find this user.config file?
thanks
OK, i found where user.config file is saved. There is anyway i can change it to be saved in application folder rather than user directory?
thanks
Short of writing your own settings provider, no, there is no way to change this.
Raghavendra explains why in his Client Settings FAQ:
http://blogs.msdn.com/rprabhu/articles/433979.aspx
Just out of curiosity, why do you want to change this path? It will break your application if you run under a normal user account, since the directory where the application is installed is normally read-only for non-admin type accounts.
Best regards,
Johan Stenberg
Can anyone point me in the direciton of doing this stuff using the .NET Framework using C#. Right now I am just trying to figure out how to read settings from the app.config file but having difficulties and there seems to be a lack of info about this.
Thanks
Martin
Are you writing your own settings provider?
Or do you want to define settings for your application?
Best regards,
Johan Stenberg
I have already set it up to use this sort of settings setup and I have used the My.Settings.Save() command after having made a series of changes.
I set it up so that there are 18 button name and link settings and 5 user name, password and limits (eg ?is admin sort of things).
I get the user name things to work perfectly, which can be changed by an admin. But the button variables don't want to work. I don't get it because I am using the exact same process to save changes to user information as with the button changing by an admin.
Please I really need help because it is getting so frustrating...
It did change this stuff while I was doing debug runs of the program, but once published or not run by VB.NET the program just doesn't want to cooperate.