zoukankan      html  css  js  c++  java
  • 配置管理应用程序块(Configuration Application Block)

    备忘:

    app.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        
    <configSections>
            
    <section name="fileSystemStorageProvider" type="ConfigurationDemo.FileSystemStorageSettings, ConfigurationDemo" />
            
    <section name="fileSystemStorageProviderOther" type="ConfigurationDemo.FileSystemStorageProviderSectionHandler, ConfigurationDemo" />
        
    </configSections>
        
    <fileSystemStorageProvider Root="D:\temp\FileStorage\Core" FolderPattern="yyyy\MM\dd" ApDocConverterTimeout="300">
        
    </fileSystemStorageProvider>
        
    <fileSystemStorageProviderOther>
            
    <param name="Root" value="D:\temp\FileStorage\Core" />
            
    <param name="FolderPattern" value="yyyy\MM\dd" />
            
    <param name="apDocConverterTimeout" value="300" />
        
    </fileSystemStorageProviderOther>
    </configuration>

    读配置文件:

    1,

    类:FileSystemStorageSettings

    using System.Configuration;

    namespace ConfigurationDemo
    {
        
    public class FileSystemStorageSettings : ConfigurationSection
        {
            [ConfigurationProperty(
    "Root")]
            
    public string Root
            {
                
    get { return (string)this["Root"]; }
                
    set 
                {
                    
    this["Root"= value;
                }
            }

            [ConfigurationProperty(
    "FolderPattern")]
            
    public string FolderPattern
            {
                
    get { return (string)this["FolderPattern"]; }
                
    set
                {
                    
    this["FolderPattern"= value;
                }
            }

            [ConfigurationProperty(
    "ApDocConverterTimeout")]
            
    public short ApDocConverterTimeout
            {
                
    get { return (short)this["ApDocConverterTimeout"]; }
                
    set
                {
                    
    this["ApDocConverterTimeout"= value;
                }
            }
        }
    }
    FileSystemStorageSettings setting = ConfigurationManager.GetSection("fileSystemStorageProvider"as FileSystemStorageSettings;
                
    if(setting != null)
                {
                    
    //...
                }

    2,

    类:

    FileSystemStorageSettingsOther
    using System;
    using System.Configuration;
    using System.Xml;

    namespace ConfigurationDemo
    {
        
    public class FileSystemStorageSettingsOther
        {
            
    private readonly string root;
            
    private readonly string folderPattern;
            
    private readonly short apDocConverterTimeout;

            
    public FileSystemStorageSettingsOther(string root, string folderPattern, string apDocConverterTimeout)
            {
                
    this.root = root;
                
    this.folderPattern = folderPattern;

                
    if (!short.TryParse(apDocConverterTimeout, out this.apDocConverterTimeout))
                    
    this.apDocConverterTimeout = 30;
            }

            
    public string Root
            {
                
    get {return root;}
            }

            
    public string FolderPattern
            {
                
    get {return folderPattern;}
            }

            
    public short ApDocConverterTimeout
            {
                
    get { return apDocConverterTimeout; }
            }
        }

        
    public class FileSystemStorageProviderSectionHandler : IConfigurationSectionHandler
        {
            
    public object Create(object parent, object configContext, XmlNode section)
            {
                
    try
                {
                    XmlElement rootNode 
    = (XmlElement)section.SelectSingleNode("param[@name='Root']");
                    
    string root = rootNode.Attributes["value"].InnerText;

                    XmlElement folderPatternNode 
    = (XmlElement)section.SelectSingleNode("param[@name='FolderPattern']");
                    
    string folderPattern = folderPatternNode.Attributes["value"].InnerText;

                    XmlElement apDocConverterTimeoutNode 
    = (XmlElement)section.SelectSingleNode("param[@name='apDocConverterTimeout']");
                    
    string apDocConverterTimeout;
                    
    if (apDocConverterTimeoutNode == null)
                        apDocConverterTimeout 
    = null;
                    
    else
                        apDocConverterTimeout 
    = apDocConverterTimeoutNode.Attributes["value"].InnerText;

                    
    return new FileSystemStorageSettingsOther(root, folderPattern, apDocConverterTimeout);
                }
                
    catch (Exception ex)
                {
                    
    throw ex;
                }
            }
        }
    }

    调用:

    FileSystemStorageSettingsOther setting = ConfigurationManager.GetSection("fileSystemStorageProviderOther"as FileSystemStorageSettingsOther;

                
    if(setting != null)
                {
                    
    //...
                }

    写入配置文件:

    1,
              Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.Sections.Remove(
    "EditorSettings");
                    config.Sections.Add(
    "EditorSettings", configData);                
                    config.Save();
     

    2,  ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = @"E:\EntLib3Src\Quick Starts\Configuration-Migration\CS\ConfigurationMigrationQuickStart\app.config";

        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        config.Sections.Remove("EditorSettings");
        config.Sections.Add("EditorSettings", configData);
        config.Save();

  • 相关阅读:
    oracle如何实现自增?----用序列sequence的方法来实现
    win7旗舰版安装 oracle 10g 不能进入图形界面的问题
    MBA都需要学习哪些课程
    查看Oracle当前用户下的(表视图,同义词...)
    辽宁省全国计算机等级考试 网上报名须知
    大学毕业之后的几年 你能考哪些证书
    plsql启动报 Using filter for all users can lead to poor perform
    hive web界面管理
    hive常用命令
    hive-site.xml
  • 原文地址:https://www.cnblogs.com/icebutterfly/p/2133881.html
Copyright © 2011-2022 走看看