Saving apps settings in INI file
Saving apps settings in INI file
Saving apps settings in INI file
Saving apps settings in INI file
Saving apps settings in INI file
Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file
Saving apps settings in INI file Saving apps settings in INI file
Saving apps settings in INI file
Go Back  Xtreme Visual Basic Talk > > > Saving apps settings in INI file


Reply
 
Thread Tools Display Modes
  #1  
Old 11-09-2007, 09:54 AM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default Saving apps settings in INI file


I used to save my apps settings in ini file, now i have started using VB.NET
does VB.NET offre a class to read/write ini file or should i use my old VB6 class?
__________________
Visit my site
Reply With Quote
  #2  
Old 11-09-2007, 10:57 AM
torpkevuk's Avatar
torpkevuk torpkevuk is offline
Contributor
 
Join Date: Oct 2003
Location: Nebraska, USA
Posts: 640
Default

rather than a .ini file, why not use a .config file, basically an xml file that is saved in C:\Documents and Settings\<user>\Application Data\<your project name>

heres a class for it:
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

and heres how to use it:

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Put this in form load Dim a As New AppSettings(AppSettings.Config.PrivateFile) End Sub Private Function SaveSettings() a.SaveSetting("SettingName", value) End Function Private Function LoadSettings() dim value as String = a.GetSetting("SettingName") End Function

hope that helps
Reply With Quote
  #3  
Old 11-09-2007, 02:40 PM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default

Thanks, but i want to work with other app which saves its settings in ini file so i want to know if .NET has calss read/write ini files
__________________
Visit my site
Reply With Quote
  #4  
Old 11-09-2007, 02:48 PM
torpkevuk's Avatar
torpkevuk torpkevuk is offline
Contributor
 
Join Date: Oct 2003
Location: Nebraska, USA
Posts: 640
Default

I would advise looking over this post.. read everything, but pay particular attention to Atma's post at the end of the thread

Troubles with ini file
Reply With Quote
  #5  
Old 11-09-2007, 03:03 PM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default

Thanks for this useful link.
according to this article
Quote:
Microsoft left out any kind of native support for this storage solution in .NET
that is what i want to know, therefore i have to stick in my own solution!!
__________________
Visit my site
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 Off
HTML code is Off

Forum Jump

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 apps settings in INI file
Saving apps settings in INI file
Saving apps settings in INI file Saving apps settings in INI file
Saving apps settings in INI file
Saving apps settings in INI file
Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file Saving apps settings in INI file
Saving apps settings in INI file
Saving apps settings in INI file
 
Saving apps settings in INI file
Saving apps settings in INI file
 
-->