Demo to read a customer's own config file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace ConfigrationTest
{
    
public class Program
    {
        
static void Main(string[] args)
        {
            
string fileName = "MyOwnLog4Net.xml";
            
string fullPath = Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).AbsolutePath);
            
string configPath = Path.Combine(fullPath, fileName);
            System.Configuration.ExeConfigurationFileMap fileMap 
= new System.Configuration.ExeConfigurationFileMap();
            fileMap.ExeConfigFilename 
= configPath;
            var config 
= ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            var configSetion 
= config.GetSection("syncManagerSettings"as SynchronizationManagerConfigSection;
            Console.Write(configSetion.syncMangerSettings[
"timeUntilFirstSync"].Value);

            Console.Read();

            
//var map = new ExeConfigurationFileMap();
        }
    }

    
public class SynchronizationManagerConfigSection : ConfigurationSection
    {
        [ConfigurationProperty(
"namedSettings", IsKey = false, IsRequired = false)]
        
public KeyValueConfigurationCollection syncMangerSettings
        {
            
get { return (KeyValueConfigurationCollection)base["namedSettings"]; }
        }
    }
}

 

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
<section name="syncManagerSettings"
         type="ConfigrationTest.SynchronizationManagerConfigSection,ConfigrationTest" />
  </configSections>
  
   <syncManagerSettings>
    <namedSettings>
      <!--All synchronization timings are in milliseconds-->

      <add key="timeUntilFirstSync" value="1000" />

   </namedSettings>

  </syncManagerSettings>
  
</configuration>

 

 

posted on 2011-06-20 15:27  Paul_  阅读(234)  评论(0编辑  收藏  举报

导航