FileConfigurationSource的运行时读写配置文件

原文链接: http://www.cnblogs.com/ycat/archive/2006/08/19/481103.html
    如果涉及运行时写配置文件则最好将配置文件分开存储,因为.NET2.0的配置管理机制将监听所有配置文件的更改,如果出现多ConfigurtionSource情况时可能会导致更新失败。
    App.Config是必须的,它是EntLib配置管理的入口,其中enterpriseLibrary.ConfigurationSource(ConfigurationSection)指明My.Config的确切位置。
    要在运行时成功保存的自定义ConfigurationSection(派生于SerializableConfigurationSection)需要注意:
      1.  FileConfigurationSource的Save方法用于EntLib内部调用不宜直接调用,保存应直接将一个Section用Add方法加入到Section集合中。(在Add方法中会调用Save方法,而在Save方法中会调用.Net20提供的Configuration先Remove具有同名的节,然后再添加给定节。)
      
           2.  不能直接将运行时的Section保存,需要为其实现一个Clone方法,创建一个Section的副本,然后用这个副本作为参数调用Add方法。通过Reflector查看Configurtion的Sections管理机制可以发现,如果直接使用Section其SectionInfomation.Atteched=true(内部),这将导致InvalidOperationException异常。实现Clone方法避免这一标志被置位。见如下代码:

None.gif
 1 None.gif      public   class  MySettings : SerializableConfigurationSection
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        private const string _configSourceName = "MyConfigurationSource";
 4InBlock.gif        private const string SectionName = "MyConfiguration";
 5InBlock.gif
 6InBlock.gif        private static MySettings defaultInstance;
 7InBlock.gif
 8InBlock.gif        ..
 9InBlock.gif
10InBlock.gif        //为了运行时保存配置文件的内容定义一些变量
11InBlock.gif        private static FileConfigurationSource _fileConfigurationSource=null;
12InBlock.gif        private static string _configFileName = string.Empty;
13InBlock.gif
14InBlock.gif        
15InBlock.gif
16InBlock.gif        //配置机制会自动加载配置片断,由EnterpriseLibrary for .Net 2.0框架封装
17InBlock.gif        public static MySettings Default
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            get
20ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
21InBlock.gif                if (defaultInstance == null)
22ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
23InBlock.gif                    IConfigurationSource cs = ConfigurationSourceFactory.Create(_configSourceName);
24InBlock.gif                    defaultInstance = (MySettings)cs.GetSection(SectionName);
25InBlock.gif
26InBlock.gif                    if (defaultInstance != null)
27ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
28InBlock.gif                        _fileConfigurationSource = cs as FileConfigurationSource;
29InBlock.gif
30InBlock.gif                        if (_fileConfigurationSource != null)
31ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
32InBlock.gif                            ConfigurationSourceSection configurationSourceSection = ConfigurationSourceSection.GetConfigurationSourceSection();
33InBlock.gif                            FileConfigurationSourceElement objectConfiguration = configurationSourceSection.Sources.Get(_configSourceName) as FileConfigurationSourceElement;
34InBlock.gif                            _configFileName = objectConfiguration.FilePath;//得到外部配置文件的路径
35InBlock.gif
36ExpandedSubBlockEnd.gif                        }

37ExpandedSubBlockEnd.gif                    }

38InBlock.gif
39ExpandedSubBlockEnd.gif                }

40InBlock.gif                return defaultInstance;
41ExpandedSubBlockEnd.gif            }

42ExpandedSubBlockEnd.gif        }

43InBlock.gif
44InBlock.gif
45InBlock.gif        
46InBlock.gif
47InBlock.gif
48InBlock.gif        public override bool IsReadOnly()
49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
50InBlock.gif            return false;
51ExpandedSubBlockEnd.gif        }

52InBlock.gif
53InBlock.gif        public MySettings Clone()
54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
55InBlock.gif
56InBlock.gif            StringBuilder output = new StringBuilder();
57InBlock.gif            XmlWriterSettings wSettings = new XmlWriterSettings();
58InBlock.gif            wSettings.Indent = true;
59InBlock.gif
60InBlock.gif            using(XmlWriter writer = XmlWriter.Create(output,wSettings))
61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
62InBlock.gif                WriteXml(writer);
63InBlock.gif                writer.Flush();
64InBlock.gif                writer.Close();
65ExpandedSubBlockEnd.gif            }

66InBlock.gif
67InBlock.gif            MySettings dcs = new MySettings();
68InBlock.gif
69InBlock.gif            XmlReaderSettings rSetting = new XmlReaderSettings();
70InBlock.gif            rSetting.CloseInput = true;
71InBlock.gif
72InBlock.gif            StringReader sr = new StringReader(output.ToString());
73InBlock.gif            using (XmlReader reader = XmlReader.Create(sr, rSetting))
74ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
75InBlock.gif                dcs.ReadXml(reader);
76InBlock.gif                reader.Close();
77ExpandedSubBlockEnd.gif            }

78InBlock.gif            sr.Close();
79InBlock.gif
80InBlock.gif
81InBlock.gif            return dcs;
82ExpandedSubBlockEnd.gif        }

83InBlock.gif
84InBlock.gif        public static void Save( MySettings dcs )
85ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
86InBlock.gif
87InBlock.gif            if (_fileConfigurationSource != null)
88InBlock.gif                _fileConfigurationSource.Add(new FileConfigurationParameter(_configFileName), SectionName, dcs.Clone());
89InBlock.gif
90ExpandedSubBlockEnd.gif        }

91InBlock.gif
92InBlock.gif        
93InBlock.gif        
94ExpandedBlockEnd.gif    }

95 None.gif
None.gif

转载于:https://www.cnblogs.com/ycat/archive/2006/08/19/481103.html

猜你喜欢

转载自blog.csdn.net/weixin_30363509/article/details/94798942