Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings
Saving and Retrieving Application Settings Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
Go Back  Xtreme Visual Basic Talk > > > > Saving and Retrieving Application Settings


Reply
 
Thread Tools Display Modes
  #1  
Old 12-29-2003, 08:36 PM
reboot's Avatar
rebootSaving and Retrieving Application Settings reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,614
Default Saving and Retrieving Application Settings


First of all a config file synopsis.

With the advent of .Net we seem to have come full circle in regard to application settings. Back in the pre-95 days Microsoft invented ini files, and told us they were good. And so they were. Then along came 95 and NT, and Microsoft said no, the registry is the way to go, forget ini files. And so we did. Now in .Net we're told to use config files, which is basically the same concept as an ini file, albeit being XML, it’s an ini file on steroids.

You’ll notice in your apps project directory a file named app.config and in your project/bin directory a file named projectname.exe.config. .Net provides a System.Configuration.AppSettingsReader Class to “read” settings from app.config. While there is a GetValue method for reading you’ll find no matching SetValue method for writing. Finding this very unhandy, I decided to write my own that included a Write method.

Originally, I had mine writing to the projectname.exe.config file, not knowing any better, then noticed that app.config overwrites this file every time you run in the IDE. Even though it works fine in a compiled executable running outside the IDE, after doing some research, I discovered that I shouldn’t have been writing to that file in the first place. Microsoft recommends that you do not have files in the Program Files directory writeable to users as this is a security risk. They further recommend that you store your configuration files in another location, preferably in the Application Data folder in the Documents and Settings folder.

And so the following code was born. Since I’ve always rather liked the ability to use the Current_User/Local_Machine sections in the registry to have, respectively, individual settings and shared settings, I included this functionality in the class. I also used the familiar GetSetting/SaveSetting terminology from VB6. There are probably several areas for improvement, more error handling, possibly the ability to save/retrieve data types other than just string, etc, but I’ll leave that to you for now.
Code:
Imports System 'a class for reading and writing XML style application settings 'sample usage: ' Create the object - ' Dim a As New AppSettings(AppSettings.Config.PrivateFile) ' Retrieve a setting - ' Dim myString As String = a.GetSetting("SettingName") ' Save a setting - ' a.SaveSetting("SettingName", value) ' Public Class AppSettings Private _configFileName As String 'local var to hold the config file name Private _configFileType As Config 'local var to hold the config file type (private or shared) 'config file options Public Enum Config SharedFile 'all users use the same config file PrivateFile 'each user has their own config file End Enum 'constructor Public Sub New(ByVal ConfigFileType As Config) _configFileType = ConfigFileType 'remember this setting InitializeConfigFile() 'setup the filename and location End Sub 'initialize the apps config file, create it if it doesn't exist Private Sub InitializeConfigFile() Dim sb As New Text.StringBuilder 'build the path\filename depending on the location of the config file Select Case _configFileType Case Config.PrivateFile 'each user has their own personal settings 'use "\documents and settings\username\application data" for the config file directory sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) Case Config.SharedFile 'all users share the same settings 'use "\documents and settings\All Users\application data" for the config file directory sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)) End Select 'add the product name sb.Append("\") sb.Append(Application.ProductName) 'create the directory if it isn't there If Not IO.Directory.Exists(sb.ToString) Then IO.Directory.CreateDirectory(sb.ToString) End If 'finish building the file name sb.Append("\") sb.Append(Application.ProductName) sb.Append(".config") _configFileName = sb.ToString 'completed config filename 'if the file doesn't exist, create a blank xml If Not IO.File.Exists(_configFileName) Then Dim fn As New IO.StreamWriter(IO.File.Open(_configFileName, IO.FileMode.Create)) fn.WriteLine("<?xml version=""1.0"" encoding=""utf-8""?>") fn.WriteLine("<configuration>") fn.WriteLine(" <appSettings>") fn.WriteLine(" <!-- User application and configured property settings go here.-->") fn.WriteLine(" <!-- Example: <add key=""settingName"" value=""settingValue""/> -->") fn.WriteLine(" </appSettings>") fn.WriteLine("</configuration>") fn.Close() 'all done End If End Sub 'get an application setting by key value Public Function GetSetting(ByVal key As String) As String 'xml document object Dim xd As New Xml.XmlDocument 'load the xml file xd.Load(_configFileName) 'query for a value Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode( _ "/configuration/appSettings/add[@key=""" & key & """]") 'return the value or nothing if it doesn't exist If Not Node Is Nothing Then Return Node.Attributes.GetNamedItem("value").Value Else Return Nothing End If End Function 'save an application setting, takes a key and a value Public Sub SaveSetting(ByVal key As String, ByVal value As String) 'xml document object Dim xd As New Xml.XmlDocument 'load the xml file xd.Load(_configFileName) 'get the value Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _ "/configuration/appSettings/add[@key=""" & _ key & """]"), Xml.XmlElement) If Not Node Is Nothing Then 'key found, set the value Node.Attributes.GetNamedItem("value").Value = value Else 'key not found, create it Node = xd.CreateElement("add") Node.SetAttribute("key", key) Node.SetAttribute("value", value) 'look for the appsettings node Dim Root As Xml.XmlNode = xd.DocumentElement.SelectSingleNode("/configuration/appSettings") 'add the new child node (this key) If Not Root Is Nothing Then Root.AppendChild(Node) Else Try 'appsettings node didn't exist, add it before adding the new child Root = xd.DocumentElement.SelectSingleNode("/configuration") Root.AppendChild(xd.CreateElement("appSettings")) Root = xd.DocumentElement.SelectSingleNode("/configuration/appSettings") Root.AppendChild(Node) Catch ex As Exception 'failed adding node, throw an error Throw New Exception("Could not set value", ex) End Try End If End If 'finally, save the new version of the config file xd.Save(_configFileName) End Sub End Class

Last edited by reboot; 12-29-2003 at 11:39 PM. Reason: Removed some redundant code
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving Multiple values from list box into a database and retrieving it sundmah Database and Reporting 2 11-26-2003 04:18 PM
Saving and retrieving text from a blob field in MYSQL. tdoman Database and Reporting 0 11-11-2003 12:19 PM
saving and retrieving delimited contents. shab General 3 03-22-2003 05:55 AM
Word templates - saving information & retrieving from desktop Sully Word, PowerPoint, Outlook, and Other Office Products 1 09-09-2002 07:20 AM
saving & retrieving images in SQL Server & Access rajvijaymohan General 2 06-11-2001 05:56 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
 
Saving and Retrieving Application Settings
Saving and Retrieving Application Settings
 
-->