Results 1 to 9 of 9

Thread: Writing to the app.config file

  1. #1

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    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?

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  3. #3

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I just rolled my own AppSettingsWriter class.

    VB Code:
    1. Public Class AppSettingsWriter
    2.  
    3.     Public Sub SetValue(ByVal key As String, ByVal value As Object)
    4.         Dim ds As New DataSet()
    5.         ds.ReadXml(GetConfigFilename)
    6.         Dim dr() As DataRow = ds.Tables("add").Select(String.Concat("key='", key, "'"))
    7.         dr(0).Item("value") = value
    8.         ds.WriteXml(GetConfigFilename)
    9.         ds.Clear()
    10.     End Sub
    11.  
    12.     Private Function GetConfigFilename() As String
    13.         Return String.Concat(Path.GetFileName(Application.ExecutablePath), ".config")
    14.     End Function
    15.  
    16. End Class
    17.  
    18. 'syntax
    19.         Dim aw As New AppSettingsWriter()
    20.         aw.SetValue("WindowState", CType(Me.WindowState, Integer))
    21.         aw.SetValue("height", Me.Height)
    22.         aw.SetValue("Width", Me.Width)

  4. #4
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    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.

  5. #5

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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?

  6. #6

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is the code I used:

    VB Code:
    1. Dim xw As New XmlTextWriter(GetConfigFilename, Encoding.UTF8)
    2.         xw.WriteStartDocument()
    3.         xw.Indentation = 5
    4.         xw.WriteStartElement("configuration")
    5.         xw.WriteStartElement("appSettings")
    6.         xw.WriteStartElement("add")
    7.         xw.WriteAttributeString("key", key)
    8.         xw.WriteAttributeString("value", value)
    9.         xw.WriteEndElement()
    10.         xw.WriteEndElement()
    11.         xw.WriteEndElement()
    12.         xw.WriteEndDocument()
    13.         xw.Close()

    I hadn't used the XMlTextWriter before so be gentle

  7. #7
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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 :/

  8. #8

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  9. #9
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    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
  •  



Click Here to Expand Forum to Full Width