|
-
Oct 4th, 2002, 11:50 AM
#1
Writing to the app.config file
Is there an internal way to write or set values in the app.config file? Everything seems to be read only. Or is it better to have a seperate file for read/write values? I figured I'd save my resize info in there but then thought twice when there isn't a default way to save values only read them. I could open it as a dataset or xml document I guess. What is everyone else doing?
-
Oct 4th, 2002, 11:57 AM
#2
Frenzied Member
Use serialization to write a binary or an XML file with all your settings that you want to persist.
Dont gain the world and lose your soul
-
Oct 4th, 2002, 12:46 PM
#3
I just rolled my own AppSettingsWriter class.
VB Code:
Public Class AppSettingsWriter
Public Sub SetValue(ByVal key As String, ByVal value As Object)
Dim ds As New DataSet()
ds.ReadXml(GetConfigFilename)
Dim dr() As DataRow = ds.Tables("add").Select(String.Concat("key='", key, "'"))
dr(0).Item("value") = value
ds.WriteXml(GetConfigFilename)
ds.Clear()
End Sub
Private Function GetConfigFilename() As String
Return String.Concat(Path.GetFileName(Application.ExecutablePath), ".config")
End Function
End Class
'syntax
Dim aw As New AppSettingsWriter()
aw.SetValue("WindowState", CType(Me.WindowState, Integer))
aw.SetValue("height", Me.Height)
aw.SetValue("Width", Me.Width)
-
Oct 5th, 2002, 09:14 PM
#4
Hyperactive Member
The world loves datasets - it's official. I would recommend using the XmlWriter for fast efficient writing and updating of keys in your app.config.
-
Oct 6th, 2002, 01:43 AM
#5
Well I tried your suggestion with the XMLTextWriter but that was more trouble than its worth because it tries to write the whole file not just the parts I want to change. Unless you have an example for me?
-
Oct 6th, 2002, 01:49 AM
#6
Here is the code I used:
VB Code:
Dim xw As New XmlTextWriter(GetConfigFilename, Encoding.UTF8)
xw.WriteStartDocument()
xw.Indentation = 5
xw.WriteStartElement("configuration")
xw.WriteStartElement("appSettings")
xw.WriteStartElement("add")
xw.WriteAttributeString("key", key)
xw.WriteAttributeString("value", value)
xw.WriteEndElement()
xw.WriteEndElement()
xw.WriteEndElement()
xw.WriteEndDocument()
xw.Close()
I hadn't used the XMlTextWriter before so be gentle
-
Oct 6th, 2002, 01:51 AM
#7
Fanatic Member
The docs for XMLWriter/et al. make my brain hurt bad. I want to start playing with XML because I can see a lot of applications for it in my current project but the docs are a bit intimidating for me :/
-
Oct 6th, 2002, 01:55 AM
#8
I hear ya I've only tested the water really. I never used it before .NET really and so far I mainly only work with it through a dataset.
-
Oct 6th, 2002, 08:36 AM
#9
Hyperactive Member
I have an example at work which I will post tomorrow when I go into the office 
The method as I recall involves creating a node which is an xpath selection of the node you are looking for. Then changing it and saving the details.
I've read a couple of books on XML and it still makes my brain hurt too. I always get the impression when reading about it that the guy writing the article/book thinks xml is simple and sees no need to explain it as simply as possible.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|