• 自定义 ConfigurationSection,ConfigurationSectionGroup [web.config操作]


    数据定义
     1   public interface ICustomConfigurationGroup{}
     2     public interface ICustomConfigurationSection{} 
     3 
     4     public class CustomConfigurationGroup : ConfigurationSectionGroup, ICustomConfigurationGroup
     5     {       
     6     }
     7 
     8     public class CustomConfigurationSection : ConfigurationSection, ICustomConfigurationSection
     9     {
    10         [ConfigurationProperty("id")]
    11         public int Id
    12         {
    13             get { return (int)this["id"]; }
    14             set { this["id"] = value; }
    15         }
    16     
    17         [ConfigurationProperty("name")]
    18         public string Name
    19         {
    20             get { return (string)this["name"]; }
    21             set {  this["name"]= value; }
    22         }
    23 
    24         [ConfigurationProperty("time")]
    25         public DateTime Time
    26         {
    27             get { return (DateTime)this["time"]; }
    28             set { this["time"] = value; }
    29         }      
    30     }
    ConfigHelper
     1     //http://msdn.microsoft.com/zh-cn/library/ms151451.aspx
     2     public class ConfigHelper
     3     {
     4 
     5         public static IEnumerable<T> GetConfigurationGroups<T>(string path) where T : ConfigurationSectionGroup,
     6                                                                                       ICustomConfigurationGroup
     7         {
     8             Configuration webconfig = GetConfiguration(CreateFileMap_Custom_Config, path);
     9             foreach (ConfigurationSectionGroup group in webconfig.SectionGroups)
    10                 if (typeof(T).IsAssignableFrom(group.GetType()))
    11                 {
    12                     yield return (T)group;
    13                 }
    14         }
    15 
    16         public static IEnumerable<T> GetConfigurationSections<T>(ConfigurationSectionGroup sectionGroup) where 
    17                                                                                      T : ConfigurationSection, 
    18                                                                                          ICustomConfigurationSection
    19         {
    20 
    21             foreach (var section in sectionGroup.Sections)
    22                 if (typeof(T).IsAssignableFrom(section.GetType()))
    23                 {
    24                     yield return (T)section;
    25                 }
    26         }
    27 
    28 
    29         //相对路径:"/","/config"  不包含文件名.       
    30         public static Configuration GetConfiguration(Func<WebConfigurationFileMap> BuildFileMap,string path)
    31         {
    32             Configuration webconfig = WebConfigurationManager.OpenMappedWebConfiguration(BuildFileMap(), path);
    33             return webconfig;
    34         } 
    35 
    36         public static WebConfigurationFileMap CreateFileMap_Custom_Config()
    37         {
    38             WebConfigurationFileMap fileMap = new WebConfigurationFileMap();
    39             string physDir = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
    40             VirtualDirectoryMapping vDirMapBase = new VirtualDirectoryMapping(physDir, true, "web.config");
    41             fileMap.VirtualDirectories.Add("/", vDirMapBase);
    42 
    43             VirtualDirectoryMapping vDirMap = new VirtualDirectoryMapping(physDir + "config", false, "custom.config");
    44             fileMap.VirtualDirectories.Add("/config", vDirMap);
    45 
    46             vDirMap = new VirtualDirectoryMapping(physDir + "config\\app", false, "custom.config");
    47             fileMap.VirtualDirectories.Add("/config/app", vDirMap);
    48             vDirMap = new VirtualDirectoryMapping(physDir + "config\\cms", false, "custom.config");
    49             fileMap.VirtualDirectories.Add("/config/cms", vDirMap);
    50 
    51 
    52             return fileMap;
    53         } 
    54         public static WebConfigurationFileMap CreateFileMap_App_Config()
    55         {
    56             WebConfigurationFileMap fileMap = new WebConfigurationFileMap();
    57             string physDir = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
    58             VirtualDirectoryMapping vDirMapBase = new VirtualDirectoryMapping(physDir, true, "web.config");
    59             fileMap.VirtualDirectories.Add("/", vDirMapBase);
    60 
    61             VirtualDirectoryMapping vDirMap = new VirtualDirectoryMapping(physDir + "config", false, "app.config");
    62             fileMap.VirtualDirectories.Add("/config", vDirMap);
    63 
    64             vDirMap = new VirtualDirectoryMapping(physDir + "config\\app", false, "app.config");
    65             fileMap.VirtualDirectories.Add("/config/app", vDirMap);
    66             vDirMap = new VirtualDirectoryMapping(physDir + "config\\cms", false, "app.config");
    67             fileMap.VirtualDirectories.Add("/config/cms", vDirMap);
    68 
    69 
    70             return fileMap;
    71         }
    72 
    73     }
    ConfigUtil
     1   public class ConfigUtil
     2         {
     3 
     4             public static void Create()
     5             {
     6 
     7                 Configuration config = ConfigHelper.GetConfiguration(ConfigHelper.CreateFileMap_Custom_Config,"/config/app/");
     8 
     9                 CustomConfigurationSection data = new CustomConfigurationSection();
    10                 data.Id = 111;
    11                 data.Name = "one";
    12                 data.Time = DateTime.Now;
    13 
    14                 config.SectionGroups.Add("group", new CustomConfigurationGroup());
    15                 config.SectionGroups["group"].Sections.Add("add", data);
    16 
    17 
    18 
    19                 config.Save(ConfigurationSaveMode.Minimal);
    20             }
    21 
    22             public static CustomConfigurationSection Read()
    23             {
    24 
    25                 Configuration config = ConfigHelper.GetConfiguration(ConfigHelper.CreateFileMap_Custom_Config, "/config/app/");
    26                 CustomConfigurationSection data = config.SectionGroups["group"].Sections["add"] as CustomConfigurationSection;
    27 
    28                 return data;
    29             }
    30 
    31             public static void Delete()
    32             {
    33                 Configuration config = ConfigHelper.GetConfiguration(ConfigHelper.CreateFileMap_Custom_Config,"/config/app/");
    34                 //config.SectionGroups.Remove("group");
    35                 if (config.SectionGroups["group"] != null)
    36                 {
    37                     config.SectionGroups["group"].Sections.Remove("add");
    38                     //config.SectionGroups["group"].Sections.Clear();
    39                 }
    40                 config.Save(ConfigurationSaveMode.Minimal);
    41             }
    42 
    43             public static void Update()
    44             {
    45                 Configuration config = ConfigHelper.GetConfiguration(ConfigHelper.CreateFileMap_Custom_Config,"/config/app/");
    46                 
    47                 var g = config.SectionGroups["group"];
    48                 if (g == null)
    49                 {
    50                     var gg = new CustomConfigurationGroup(); 
    51                     config.SectionGroups.Add("group", gg);
    52                 }
    53 
    54                 var s = config.SectionGroups["group"].SectionGroups["groupA"];
    55                 if (s == null)
    56                 {
    57                     var gg = new CustomConfigurationGroup();                  
    58                     config.SectionGroups["group"].SectionGroups.Add("groupA", gg);
    59                 }
    60                 var ss = config.SectionGroups["group"].SectionGroups["groupA"].Sections["ChildB"];
    61                 if (ss == null)
    62                 {  
    63                       CustomConfigurationSection data = new CustomConfigurationSection();
    64                       data.Id = 101;
    65                       data.Name = "wangwang";
    66                       data.Time = DateTime.Now;              
    67                     config.SectionGroups["group"].SectionGroups["groupA"].Sections.Add("ChildB", data);
    68                 }
    69 
    70                 config.Save(ConfigurationSaveMode.Minimal);
    71             }
    72         }
     public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ConfigUtil.Create();
                ConfigUtil.Update();
            }
        }
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <configuration>
     3     <configSections>
     4         <sectionGroup name="group" type="AppConfig.CustomConfigurationGroup, AppConfig, Version=1.0.0.0, 
    Culture=neutral, PublicKeyToken=null
    " > 5 <section name="add" type="AppConfig.CustomConfigurationSection, AppConfig, Version=1.0.0.0,
    Culture=neutral, PublicKeyToken=null
    " /> 6 </sectionGroup> 7 <sectionGroup name="group"> 8 <sectionGroup name="groupA" type="AppConfig.CustomConfigurationGroup, AppConfig, Version=1.0.0.0,
    Culture=neutral, PublicKeyToken=null
    " > 9 <section name="ChildB" type="AppConfig.CustomConfigurationSection, AppConfig, Version=1.0.0.0,
    Culture=neutral, PublicKeyToken=null
    " /> 10 </sectionGroup> 11 </sectionGroup> 12 </configSections> 13 <group> 14 <add id="111" name="one" time="11/24/2012 22:43:53" /> 15 <groupA> 16 <ChildB id="101" name="wangwang" time="11/24/2012 22:43:53" /> 17 </groupA> 18 </group> 19 </configuration>

    [注]:  http://msdn.microsoft.com/zh-cn/library/ms151451.aspx
  • 相关阅读:
    Tomcat启动流程简析
    Tomcat的启停脚本源码解析
    Servlet规范
    CCNA
    CCNA-Part 6
    MYSQL 使用基础
    CCNA-Part5
    CCNA-Part4 -网络层
    MySQL 字符串索引优化方案
    CCNA-Part3
  • 原文地址:https://www.cnblogs.com/AspDotNetMVC/p/2786479.html
Copyright © 2020-2023  润新知