I use the class (code bellow) to write keys on app.exe.config thats is
loaded at startup
You can choose under what key you want to store any settings (set Identifier
from de calling routine)
One remark: While developing it writes on app.exe.config but try to not load
setings on the app.config so i'll need to manuallly copy settings on
app.exe.config to app.config to work on IDE
Hope it Helps
Marcos
Form Code:
*****************
Imports System.Configuration
Imports myApp.myUtilities
Public Identifier as Integer
Load....
Me.Width = CType(Reader.GetValue(Identifier.tostring & ".Width",
GetType(System.Int32)), Int32)
end sub
Closing....
Dim Writer As New AppSettingsWriter()
Writer(Identifier.tostring & ".Width") = CStr(Me.Width)
Writer.SaveFile()
end sub
Class:
*****************
Imports System
Imports System.Xml
Namespace myApp.myUtilities
Public Class AppSettingsWriter
Private configFileName As String
Private document As XmlDocument
Public Sub New()
Dim asmy As System.Reflection.Assembly
Dim tempName As String
asmy = System.Reflection.Assembly.GetEntryAssembly()
tempName = asmy.Location
configFileName = tempName & ".config"
document = New XmlDocument()
document.Load(configFileName)
End Sub
Default Public WriteOnly Property Value(ByVal key As String) As String
Set(ByVal Value As String)
Dim Query As String
Dim Node As XmlNode
Dim Root As XmlNode
Dim Attribute1 As XmlNode
Dim Attribute2 As XmlNode
Query = "/configuration/appSettings/add[@key=" & _
Chr(34) & key & Chr(34) & "]"
Node = document.DocumentElement.SelectSingleNode(Query)
If Not Node Is Nothing Then
Node.Attributes.GetNamedItem("value").Value = Value
Else
Node = document.CreateNode(XmlNodeType.Element, "add", "")
Attribute1 = document.CreateNode(XmlNodeType.Attribute, "key", "")
Attribute1.Value = key
Node.Attributes.SetNamedItem(Attribute1)
Attribute2 = document.CreateNode(XmlNodeType.Attribute, "value", "")
Attribute2.Value = Value
Node.Attributes.SetNamedItem(Attribute2)
Query = "/configuration/appSettings"
Root = document.DocumentElement.SelectSingleNode(Query)
If Not Root Is Nothing Then
Root.AppendChild(Node)
Else
Throw New InvalidOperationException("Não pude adicionar o nodo ao
arquivo config")
End If
End If
End Set
End Property
Public Sub SaveFile()
document.Save(configFileName)
End Sub
End Class
End Namespace
"DraguVaso" <pi**********@hotmail.com> escreveu na mensagem
news:HH*********************@phobos.telenet-ops.be...
Hi,
I found some examples for storing the FormSettings of a Form in an
XML-file, but none of these could match my criteria: What I am looking for is the
possibility to save the FormSettings of multiple Instances of 1 form.
I have a Form frmSource from which I have multiple Instances, each with a
unique Identifier. So when I Load or Close a specific instance, I want to
load or save the Settings (Top, Height, Width, etc) in an XML-file. I need
to find a way to search with the unique Identifier of the Form in the
XML-file.
Anybody had some examples of this? I thought maybe making a DataSet with
the settings stored in it and writing and reading the XML-file fromd the
DataSet? But how do I fill that DataSet than with my FormSettings?
I'm pretty new to XML (and VB.NET also), so any help would be appreciated!
Thanks in advance!!
DraguVaso