CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2003
    Posts
    4

    Using XML to write configuration files -- .INI like

    Hello. I am writing an application that has several hardcoded directory paths. I would like to write a configuration utility to write an XML based INI file. This file would be read at the execution of the program and the variables initialized. Does anyone have a link to a good reference or thread. I would really benefit from seeing some code of an actual variable being initialized with data from an external file.

    Thanks
    brazilnut

  2. #2
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    No need to write something of your own that the .NET Framework allready has.
    A while ago I wrote an artical about this on the Planet Source Code website. I will post it here also.

    Danny

    Code:
    In de .NET Framework are no build-in statements
    to access INI files. To do this, you must access the
    file directly or use the Win32 API's.
    
    In DotNet we now have Configuration Files.
    This are XML files that can be changed as needed
    without recompiling the application.
    
    There are four different kind of configuration files:
    - Machine Configuration Files
    - Application Configuration Files
    - Security Configuration Files
    - ASP.NET Configuration Files
    
    The Application Configuration File is to compare
    with an INI file. With one big difference that
    you only can Read settings, but not Write settings!
    Writing has to be done manualy.
    
    
    To use a configuration file you must add an
    'Application Configuration File' to your project.
    
    In the Visual Studio Environment this can be done by
    right clicking the project, choose 'add new item'.
    Then look for the item 'Application Configuration File'.
    This will add an 'App.Config' file to your project.
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    </configuration>
    
    To add your own settings, you must add a section
    called 'appSettings'.
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
        </appSettings>
    </configuration>
    
    Here you can store your settings as key/value pairs.
    
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings>
            <add key="Test1" value="This is the value of Test1" />
            <add key="Test2" value="This is the value of Test2" />
        </appSettings>
    </configuration>
    
    !!! BEWARE.......THE TEXT IS CASE SENSITIVE !!!
    
    'appSettings' is not the same as 'appsettings'
    Notice the captial S.
    A runtime error will occure if you make a typing error.
    
    
    An VB.NET example to read from the Configuration file.
    
    Imports System
    Imports System.Console
    
    Module Module1
    
        Sub Main()
            Try
                Dim config As Configuration.ConfigurationSettings
    
                ' Read the key:Test2
                WriteLine(config.AppSettings("Test2"))
    
                ' To read all key's
                Dim key As String
    
                For Each key In config.AppSettings.AllKeys
                    WriteLine(key & " -- " & config.AppSettings(key))
                Next
    
                WriteLine("")
    
            Catch ex As Exception
                WriteLine("")
                WriteLine(ex.ToString)
    
            Finally
                Read()
    
            End Try
        End Sub
    
    End Module
    
    
    After compiling the assembly, you also will get
    an config file in your bin directory.
    
    For example: an project called MyTest, results in
    MyTest.exe
    MyTest.exe.config
    
    The two files must be placed in the
    same directory to work.

  3. #3
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167
    DHD,

    A question for you. You know how the old good INI file has sections and each section has its own key/value pairs? Can you achieve this with the APPSETTINGS?

    -Cool Bizs

  4. #4
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    No, not with the default commands provided in the FrameWork.

    But I found an submition on the GotDotNet website of someone who wrote some code how to read different sections.

    Alternative Config file section readers

    Maybe that can help you.

    Danny

  5. #5
    Join Date
    May 1999
    Location
    Saint Paul, Minnesota, US
    Posts
    91

    Re: Using XML to write configuration files -- .INI like

    Hi ...

    I am a year late on this discussion ...

    Why would you not use the registry to manage the application configuration infomration. This seems to be the standard with all the 'pro' apps !!??

    I am working for a company and we have some apps (All Windows) that are MSVC 6.0 c++ and we are using the regsitry to store this stuff.

    .NET is coming and we are starting to get some people that now want to store the app configuration in XML in a file.

    I think there are many benefits to the regisry

    - All data is saved in one location
    - OS tool provided to edit and maintain data
    - Data is typed defined (to some extent)
    - Standard backup apps will back up the regsitry

    NOW ... if I was creating an application that was going to run on Windows and Unix then I would use the XML as a configuration medium.

    That is all.
    Peace and Love,
    Chris Macgowan

    Voting for Kerry !!


    -

    I am working for a company

  6. #6
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: Using XML to write configuration files -- .INI like

    Registry used to be the best way to save configuration information but it does not work well w/ XCOPY deployment methodology of .NET application. In fact, the idea of .NET application is to be able to zip up 1 folder and extract it to another PC and it should work without any other installation steps to be taken. This also makes it easy to move the installation from one PC to another since you can just xcopy 1 folder and it will just work.

    Another reason for XML formatted INI file is for interopability. You can write the same application for 2 diff OSes but use the same configuration module. This is still far-fetch since .NET framework is not available for other OSes yet but that does not stop you from writting the same application using a different programming language and still use the same configuration file.

    File based configuration setting is much easier to manage then registry especially for remote management. Also, it allows you to store a lot more information which registry limits per key.

    My 2 cents -
    Good Luck,
    -Cool Bizs

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

Featured