سلام
میدونم با وجود Setting در VS2005 دیگه احتیاج به هیچ کلاسی برای ذخیره تنظیمات نیست، اما گفتم این 2تا کلاس رو هم بد نیست دوستان داشته باشند:
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 ClassAppSettings
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)
Public Shared filePath As String
'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.Sp ecialFolder.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.Sp ecialFolder.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
filePath = _configFileName
'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
Imports System.Collections.Hashtable
Module modSettings
Dim myHashes As Hashtable
Sub BuildHashTable(ByVal myControl As Control, Optional ByVal isInit As Boolean = True)
If isInit Then
If myHashes Is Nothing Then myHashes = New Hashtable(myControl.Controls.Count)
myHashes.Clear()
End If
Dim i%
Dim c As New Control
If myControl Is Nothing Then Exit Sub
If myControl.Controls Is Nothing Then Exit Sub
If myControl.Controls.Count <= 0 Then Exit Sub
For i = 0 To myControl.Controls.Count - 1
myHashes.Add(myControl.Controls(i).Name, myControl.Controls(i))
BuildHashTable(myControl.Controls(i), False)
Next
End Sub
Sub SetProperty(ByVal myObject As Object, ByVal Prop As String)
If myObject Is Nothing Then Exit Sub
Dim s$
Try
s = Prop
If s <> "" Then
Select Case myObject.GetType.ToString
Case "System.Windows.Forms.TextBox"
myObject.Text = s
Case "System.Windows.Forms.Label"
myObject.Text = s
Case "System.Windows.Forms.RadioButton"
If Mid(s, 1, 1) = "1" Then
myObject.checked = True
Else
myObject.checked = False
End If
Case "System.Windows.Forms.CheckBox"
If Mid(s, 1, 1) = "1" Then
myObject.checked = True
Else
myObject.checked = False
End If
End Select
End If
Catch ex As System.Exception
End Try
End Sub
Sub LoadControls(ByVal me1 As Control)
Dim key$, value$
Dim o As Control
While Not EOF(FileNo)
Try
key = LInput(FileNo)
o = myHashes.Item(key)
value = LInput(FileNo)
SetProperty(o, value)
Catch ex As System.Exception
FClose(FileNo)
Throw New System.Exception(ex.Message)
End Try
End While
End Sub
Sub SaveControls(ByVal me1 As Control)
Dim c As Object
Dim i%, s$, t$
If me1 Is Nothing Then Exit Sub
If me1.Controls Is Nothing Then Exit Sub
If me1.Controls.Count <= 0 Then Exit Sub
For i = 0 To me1.Controls.Count - 1
c = me1.Controls(i)
Try
Select Case c.GetType.ToString
Case "System.Windows.Forms.TextBox"
RSet(c.name, c.text)
Case "System.Windows.Forms.Label"
RSet(c.name, c.text)
Case "System.Windows.Forms.RadioButton"
If c.checked Then
RSet(c.name, "1")
Else
RSet(c.name, "0")
End If
Case "System.Windows.Forms.CheckBox"
If c.checked Then
RSet(c.name, "1")
Else
RSet(c.name, "0")
End If
End Select
Catch ex As System.Exception
End Try
SaveControls(me1.Controls(i))
Next
End Sub
Public Sub SaveSettings(ByVal Cntl As Control, ByVal File$)
FileNo = FOpen(File, OpenMode.Output)
If FileNo = -1 Then Throw New System.Exception("File - " + File + " cannot be opened for writing!")
BuildHashTable(Cntl)
SaveControls(Cntl)
FClose(FileNo)
End Sub
Public Sub LoadSettings(ByVal Cntl As Control, ByVal File$)
FileNo = FOpen(File)
If FileNo = -1 Then Throw New System.Exception("File - " + File + " cannot be opened for reading!")
BuildHashTable(Cntl)
LoadControls(Cntl)
FClose(FileNo)
End Sub
Dim FileNo% = -1
Private Sub RSet(ByVal Key$, ByVal Value$)
Print(FileNo, Key & vbCrLf)
Print(FileNo, Value & vbCrLf)
End Sub
Public Function FOpen(ByVal File$, Optional ByVal Mode As Microsoft.VisualBasic.OpenMode = OpenMode.Input) As Integer
FOpen = FreeFile()
Try
FileOpen(FOpen, File, Mode)
Catch ex As System.Exception
Try
FileClose(FOpen)
Catch ex2 As System.Exception
End Try
Return -1
End Try
End Function
Public Function FClose(ByVal FILE%) As Integer
Try
FileClose(FILE)
Catch ex As System.Exception
Return -1
End Try
Return 0
End Function
Public Function LInput(ByVal FILE%, Optional ByVal STR$ = "Error - Cannot read another line from file ") As String
Try
LInput = LineInput(FILE)
Catch ex As System.Exception
Throw New System.Exception(STR)
End Try
End Function
End Module
در پناه حق موفق باشید و پرتوان