Read custom configuration file in C# (Framework 4.0)

前端 未结 5 1701
执念已碎
执念已碎 2020-12-02 20:14

I am developing an application in C# under Framework 4.0.

In my application I would like to create separate configuration file that is not the app.config file. The c

相关标签:
5条回答
  • 2020-12-02 20:45

    In the past I've used Nini - http://nini.sourceforge.net/manual.php which allows you to read / write custom configuration files (XML / Ini / Registry / .Config) at runtime.

    Hope this helps.

    0 讨论(0)
  • 2020-12-02 20:49

    My advice with configuration is create your own component to read it.
    It may ease the development if at some point you'll decide that you will be getting the configuration from another source like a database or over a web service.
    This kind of abstraction also helps you mock the configuration and increases testability (something that the .NET framework doesn't do well at all).
    If you are using XML as the configuration format I suggest using Linq to XML.
    It's easier to read and use to parse XML files.

    0 讨论(0)
  • 2020-12-02 20:49

    Or use NameValueCollection easier

    0 讨论(0)
  • 2020-12-02 21:03

    You can try Cinchoo framework for your needs. It simplifies development work by code first approach. Define a class as below,

    namespace HelloWorld
    {
        #region NameSpaces
    
        using System;
        using Cinchoo.Core.Configuration;
    
        #endregion NameSpaces
    
        [ChoConfigurationSection("sample")]
        public class SampleConfigSection : ChoConfigurableObject
        {
            #region Instance Data Members (Public)
    
            [ChoPropertyInfo("name", DefaultValue="Mark")]
            public string Name;
    
            [ChoPropertyInfo("message", DefaultValue="Hello World!")]
            public string Message;
    
            #endregion
        }
    
        static void Main(string[] args)
        {
            SampleConfigSection sampleConfigSection = new SampleConfigSection();
            Console.WriteLine(sampleConfigSection.ToString());
        }
    
    }
    

    First time, when you run the application it will creates the configuration section in [appexename].xml file as below. Then onward any changes made to this file will be picked up automatically

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="sample" type="Cinchoo.Core.Configuration.ChoNameValueSectionHandler, Cinchoo.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b7dacd80ff3e33de" />
      </configSections>
      <sample>
        <add key="name" value="Mark" />
        <add key="message" value="Hello World!" />
      </sample>
    </configuration>
    
    0 讨论(0)
  • 2020-12-02 21:11
     // Map the roaming configuration file. This
          // enables the application to access 
          // the configuration file using the
          // System.Configuration.Configuration class
          ExeConfigurationFileMap configFileMap =
            new ExeConfigurationFileMap();
          configFileMap.ExeConfigFilename = 
            roamingConfig.FilePath;
    
          // Get the mapped configuration file.
          Configuration config =
            ConfigurationManager.OpenMappedExeConfiguration(
              configFileMap, ConfigurationUserLevel.None);
    

    from http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

    0 讨论(0)
提交回复
热议问题