Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Reading Custom Config Files in C# .NET 2.0

  • 27-06-2006 11:10am
    #1
    Registered Users, Registered Users 2 Posts: 14,148 ✭✭✭✭


    Hi guys,

    I'm have a configuration file called myApp.exe.config which looks something like this:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    	<configSections>
    		<section name="directory" type="MyApp.DirectoryConfigSection, DirectoryConfigSection" />
    	</configSections>
    	<directory>
    		<add key="monitored" value="C:\Temp\bleh1" />
    		<add key="xml" value="XML Files" />
    		<add key="log" value="Logs" />	
    	</directory>
    	<appSettings>
    		<add key="title" value="MyApp Title Test Name" />
    	</appSettings>
    </configuration>
    

    I have set up the following class, inherited from ConfigurationSection with which to read the directory xml node into:
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Text;
    
    namespace MyApp
    {
        public class DirectoryConfigSection : ConfigurationSection
        {
            // Constructor
            public DirectoryConfigSection()
                : base()
            {
            }
    
            // Monitored application directory
            public String Monitored
            {
                get { return (String)base["monitored"]; }
                set { base["monitored"] = value; }
            }
    
            // Directory for placing generated
            // XML files
            public String XML
            {
                get { return (String)base["xml"]; }
                set { base["xml"] = value; }
            }
    
            // Log file directory
            public String Log
            {
                get { return (String)base["log"]; }
                set { base["log"] = value; }
            }
        }
    }
    

    And a half-arsed implementation (since I can't seem to get the bloody thing working) that attempts to read the config file, which goes something like this:
    private void GetAppSettings()
    {
    	Configuration config;
    	DirectoryConfigSection dirSection;
            ConfigurationSection conf;
    
            try
            {
            	config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
                        as Configuration;
    
                    dirSection = (DirectoryConfigSection)config.GetSection("directory");
    
                    conf = config.GetSection("directory") as ConfigurationSection;
            }
            catch(ConfigurationErrorsException exConfig)
            {
            	// Write log error here
                    Close();
            }
            catch (Exception ex)
            {
            	// Write log error here
                    Close();
            }
    }
    

    I suspect the problem is that i am not picking up the app.exe.config file since when I debug and inspect the values assigned to 'config' after its initial assignment, the 'hasPath' value is set to false. Even if I assign the file name, I cannot seem to get anything. But in any case, neither the 'dirSection' or 'conf' variables seem to populate and I have been banging my head against this for far, far too long. Perhaps I'm doing this completely arse about-face but I cannot seem to find a definitive answer on how to go about doing this.

    Anyone have any input? :confused:


Comments

  • Registered Users, Registered Users 2 Posts: 1,466 ✭✭✭Smoggy


    will you be looking to have multiple directories monitored ?

    Also are you looking to write back into the app.config ? as I see you have set sections in your class.

    If your just looking to monitor a single dir :

    <configuration>
    <appSettings>
    <add key="monitored" value="C:\Temp\bleh1" />
    <add key="xml" value="XML Files" />
    <add key="log" value="Logs" />

    Reading them in the same way you will with :

    <add key="title" value="MyApp Title Test Name" />

    Obviously if your looking to monitor multiple directories this short cut will not work.


  • Registered Users, Registered Users 2 Posts: 14,148 ✭✭✭✭Lemming


    Smoggy wrote:
    will you be looking to have multiple directories monitored ?

    Also are you looking to write back into the app.config ? as I see you have set sections in your class.

    If your just looking to monitor a single dir :

    <configuration>
    <appSettings>
    <add key="monitored" value="C:\Temp\bleh1" />
    <add key="xml" value="XML Files" />
    <add key="log" value="Logs" />

    Reading them in the same way you will with :

    <add key="title" value="MyApp Title Test Name" />

    Obviously if your looking to monitor multiple directories this short cut will not work.

    I'm monitoring a single directory, although that xml file I showed is a bit smaller than the actual one, however is structuered same as above. I put in the custom entries for logical grouping


  • Registered Users, Registered Users 2 Posts: 1,466 ✭✭✭Smoggy


    Right it seems that c# / vs.net 2005 is way different from vb.net / vs.net 2003, but summat like this should get you going :

    using System.Collections.Specialized;
    using System.Configuration;

    NameValueCollection section = (NameValueCollection)
    ConfigurationSettings.GetConfig("Directories");

    for (int i = 0; i < section.Keys.Count; i++)
    {
    string key = section.Keys;
    string name = section.GetValues (key)[0];
    }

    For a multiple directory monotoring system use the above.

    For getting single values, use the following :

    strMonDirectory = ConfigurationSettings.AppSettings["monitored"];

    VS.net 2003 doesnt allow you to write values into the .config files, they are read only and you have to look at the registry for stroring user details. This may have changed with vs.net 2005, but I doubt it.

    Hope thats of some help.


  • Registered Users, Registered Users 2 Posts: 14,148 ✭✭✭✭Lemming


    ConfigurationsSettings has been depreciated and replaced by ConfigurationManager, but I'll take a look and see if I can get inspiration from it. Thanks for the input!


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    For simplicity's sake would it not be easier to just put your config data into it's own seperate XML file and treat it as any other XML data source ?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 14,148 ✭✭✭✭Lemming


    I could stevenmu, but I'd like to get my head around configuration files if I can help it.


Advertisement