Results 1 to 10 of 10

Thread: Application.config XML config [resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    67

    Application.config XML config [resolved]

    Hey guys I'm having a HELL of a time grasping Application.config files. I understand the theories but not the implementations. Heres a great link I found http://msdn.microsoft.com/library/de...et04222003.asp

    It still doesn't help me. I'm hoping someone has a link to a real example for a basic config with perhaps some code?

    Something simple like I want to store the windows form color and say the program author in the xml file...

    So I want to store 2 strings.. ProgramColor and Author..

    How???

    HELP! I'm an XML noob
    Last edited by Iamthewalrus15; May 2nd, 2003 at 07:29 PM.

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Add these to your app.config file:

    <add key="ProgramColor" value="Green" />
    <add key="Author" value="Me" />

    Then you can reference them in your code by using:

    Dim ColorValue = System.Configuration.ConfigurationSettings.AppSettings(ProgramColor)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    67
    So there is no special things to read and write? just read and write regular old XML, just format it correctly?? I was under the impression there were some classes to streamline this??

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    67
    Originally posted by VBCrazyCoder
    Add these to your app.config file:

    <add key="ProgramColor" value="Green" />
    <add key="Author" value="Me" />

    Then you can reference them in your code by using:

    Dim ColorValue = System.Configuration.ConfigurationSettings.AppSettings(ProgramColor)
    I'm sorry I am REALLY confused on this ADD KEY thing that I keep reading about. Wouldn't we just make an XML document something like...

    <configuration>
    <applicationsettings>
    <color>Red</color>
    <author> Me </author>
    </applicationsettings>
    </configuration>


    Something like this? Where does the add key come in? I'm so lost.. There seems to be so little on this out there too :/

    Thanks again for any help

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    67
    Here let me show ya my code.. As you can see I'm way off base.. This creates an XML file (is this how we should create this???) and then trys to read it with appsettings method mentioned above. I hope the forum keeps my formatting.

    this is what the xml file looks like that it creates:
    -<Configuration>
    <appsettings><add key = "test" value ="testvalue">
    </appsettings>
    </Configuration>


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim mywriter As New Xml.XmlTextWriter("c:\test.xml", nothing)
    mywriter.WriteStartElement("Configuration")
    mywriter.WriteStartElement("appsettings")
    mywriter.WriteString("<add key = ""test"" value = ""testvalue"">")
    mywriter.WriteEndElement()
    mywriter.WriteEndElement()
    mywriter.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim sfile As New String("c:\test.xml")
    Dim myfile As New System.IO.FileStream(sfile, IO.FileMode.Open)
    Dim myreader As New Xml.XmlTextReader(myfile)
    'dostuff
    messagebox.Show(System.Configuration.ConfigurationSettings.AppSettings(test)
    myreader.Close()
    myfile.Close()
    End Sub

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Most of the time the appConfig file is created at designtime. From the project menu add new item and you should see application configuration file in the list. Another thing to remember is that in XML the attribute and element names are case sensitive so 'appsettings' and 'appSettings' are not the samething. That could save you some hassle down the road.

    The .NET framework has an object System.Configuration.AppSettingsReader or System.Configuration.ConfigurationSettings.AppSettings way of reading from the config file but you'll have to make your own to write to it.

    If you get stuck there are some floating around the web or if you want I'll post one.

  7. #7
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    The app.config file is generated by .NET when you add a new item of type Application Config. Why do you want to create your own?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    67
    CrazyCoder:
    Could I be mistaken? Isn't the intention of the application configuration file to remove the need for the registry? I'm trying to follow the .net recommendations as close as possible. In following this wouldn't we place our application config information inside of the app.config file instead of the registry? Thats why I'm tyring to make all this happen.

    Edneeis:
    If you have a link perhaps? That would be awesome!


    Thanks SOOOO much guys..

    Heres my latest failures,,

    In my second procedure i have the .appsettings command to read the value from the file... But no where do I see how to tie this to the xml file that im streaming from. hmm





    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim mywriter As New Xml.XmlTextWriter("c:\test.xml", Nothing)
    mywriter.WriteStartDocument(True)
    mywriter.WriteStartElement("Configuration")
    mywriter.WriteStartElement("appsettings")
    mywriter.WriteStartElement("add")
    mywriter.WriteAttributeString("key", "testkey")
    mywriter.WriteAttributeString("value", "testvalue")
    mywriter.WriteEndElement()
    mywriter.WriteEndElement()
    mywriter.WriteEndDocument()
    mywriter.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim sfile As New String("c:\test.xml")
    Dim myfile As New System.IO.FileStream(sfile, IO.FileMode.Open)
    Dim myreader As New Xml.XmlTextReader(myfile)
    'dostuff
    Dim test1 As String
    test1 = System.Configuration.ConfigurationSettings.AppSettings("testkey")
    MessageBox.Show(test1)
    myreader.Close()
    myfile.Close()
    End Sub

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    67
    Hey I found a GREAT class here:

    http://www.planetsourcecode.com/vb/s...=503&lngWId=10

    But let me ask you guys. Is this what MS wants? for us to store what we would have stored as registry settings now into the application.config?? If so, I sure haven't seen a whole lot about creating these files. :/

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    There is no need to specific the name because the application configuration file always follows the following naming convention:
    applicationname.exe.config

    So if the application is MyApp.exe then the config file is MyApp.exe.config and it has to be in the same folder as the exe, otherwise know as the application folder.

    I don't have a link sorry. Here I'll post what I use, I hope it doesn't confuse you worse.

    Like VBCrazyCoder said you'd normally not have to write code to make a new config file. You'd usually have it as part of your project at designtime. Although you may want to write values to it in which cause I'd recommend the XMLDocument object instead of the XMLTextWriter, its much less work.

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