Read and write configuration files FileConfigurationSource runtime

Original link: http://www.cnblogs.com/ycat/archive/2006/08/19/481103.html
    If you write a configuration file to run the best when it comes to the configuration file stored separately, because .NET2.0 configuration management system will monitor all changes to configuration files, may cause the update fails if more than ConfigurtionSource situation.
    App.Config is necessary, which is an inlet EntLib configuration management, wherein enterpriseLibrary.ConfigurationSource (ConfigurationSection) specified My.Config exact location.
    To run successfully saved custom ConfigurationSection (derived from SerializableConfigurationSection) Note:
l the Save method 1. FileConfigurationSource EntLib for internal calls should not be called directly, save directly to a Section should use the Add method is added to Section collection. (In the Add method calls the Save method, and in the Save method calls Configuration Remove .Net20 provided with a first section of the same name, and then add the given section.)
L      
           Section 2. You can not run directly save time, we need to for the realization of a Clone method to create a copy of Section, and then use this copy as calling the Add method. Reflector can be found by viewing Configurtion of Sections management mechanism, if their direct use Section SectionInfomation.Atteched = true (internal), which causes an InvalidOperationException exception. Implement the Clone method to avoid this flag is set. See the following code:

None.gif
 . 1 public class  MySettings: SerializableConfigurationSection  2 {  . 3 Private const String  _configSourceName  = " MyConfigurationSource " ;  . 4 Private const String  SectionName  = " MyConfiguration " ;  . 5  . 6 Private static  MySettings the defaultinstance;  . 7  . 8         ..  . 9 10 // To save the configuration file runtime content define some variables 11 Private static None.gif       
ExpandedBlockStart.gifContractedBlock.gif     dot.gif
InBlock.gif           
InBlock.gif           
InBlock.gif
InBlock.gif         
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
InBlock.gif          _FileConfigurationSource FileConfigurationSource = null ;
12 is Private static String  _configFileName  = String .Empty; 13 is 14 15 16 // configuration mechanism automatically load the configuration segment by EnterpriseLibrary for .Net 2.0 framework package . 17 public static  MySettings the Default 18 is { . 19 GET 20 is { 21 is IF  (the defaultinstance  == null ) 22 is { 23 is                     IConfigurationSource CS  =InBlock.gif           
InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        
InBlock.gif         
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
InBlock.gif                 
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif
InBlock.gif 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

Reproduced in: https: //www.cnblogs.com/ycat/archive/2006/08/19/481103.html

Guess you like

Origin blog.csdn.net/weixin_30363509/article/details/94798942