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:
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:
. 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
_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 =
ConfigurationSourceFactory.Create(_configSourceName);
24 defaultInstance = (MySettings)cs.GetSection(SectionName);
25
26 if (defaultInstance != null)
27 {
28 _fileConfigurationSource = cs as FileConfigurationSource;
29
30 if (_fileConfigurationSource != null)
31 {
32 ConfigurationSourceSection configurationSourceSection = ConfigurationSourceSection.GetConfigurationSourceSection();
33 FileConfigurationSourceElement objectConfiguration = configurationSourceSection.Sources.Get(_configSourceName) as FileConfigurationSourceElement;
34 _configFileName = objectConfiguration.FilePath;//得到外部配置文件的路径
35
36 }
37 }
38
39 }
40 return defaultInstance;
41 }
42 }
43
44
45
46
47
48 public override bool IsReadOnly()
49 {
50 return false;
51 }
52
53 public MySettings Clone()
54 {
55
56 StringBuilder output = new StringBuilder();
57 XmlWriterSettings wSettings = new XmlWriterSettings();
58 wSettings.Indent = true;
59
60 using(XmlWriter writer = XmlWriter.Create(output,wSettings))
61 {
62 WriteXml(writer);
63 writer.Flush();
64 writer.Close();
65 }
66
67 MySettings dcs = new MySettings();
68
69 XmlReaderSettings rSetting = new XmlReaderSettings();
70 rSetting.CloseInput = true;
71
72 StringReader sr = new StringReader(output.ToString());
73 using (XmlReader reader = XmlReader.Create(sr, rSetting))
74 {
75 dcs.ReadXml(reader);
76 reader.Close();
77 }
78 sr.Close();
79
80
81 return dcs;
82 }
83
84 public static void Save( MySettings dcs )
85 {
86
87 if (_fileConfigurationSource != null)
88 _fileConfigurationSource.Add(new FileConfigurationParameter(_configFileName), SectionName, dcs.Clone());
89
90 }
91
92
93
94 }
95
_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 =
ConfigurationSourceFactory.Create(_configSourceName);
24 defaultInstance = (MySettings)cs.GetSection(SectionName);
25
26 if (defaultInstance != null)
27 {
28 _fileConfigurationSource = cs as FileConfigurationSource;
29
30 if (_fileConfigurationSource != null)
31 {
32 ConfigurationSourceSection configurationSourceSection = ConfigurationSourceSection.GetConfigurationSourceSection();
33 FileConfigurationSourceElement objectConfiguration = configurationSourceSection.Sources.Get(_configSourceName) as FileConfigurationSourceElement;
34 _configFileName = objectConfiguration.FilePath;//得到外部配置文件的路径
35
36 }
37 }
38
39 }
40 return defaultInstance;
41 }
42 }
43
44
45
46
47
48 public override bool IsReadOnly()
49 {
50 return false;
51 }
52
53 public MySettings Clone()
54 {
55
56 StringBuilder output = new StringBuilder();
57 XmlWriterSettings wSettings = new XmlWriterSettings();
58 wSettings.Indent = true;
59
60 using(XmlWriter writer = XmlWriter.Create(output,wSettings))
61 {
62 WriteXml(writer);
63 writer.Flush();
64 writer.Close();
65 }
66
67 MySettings dcs = new MySettings();
68
69 XmlReaderSettings rSetting = new XmlReaderSettings();
70 rSetting.CloseInput = true;
71
72 StringReader sr = new StringReader(output.ToString());
73 using (XmlReader reader = XmlReader.Create(sr, rSetting))
74 {
75 dcs.ReadXml(reader);
76 reader.Close();
77 }
78 sr.Close();
79
80
81 return dcs;
82 }
83
84 public static void Save( MySettings dcs )
85 {
86
87 if (_fileConfigurationSource != null)
88 _fileConfigurationSource.Add(new FileConfigurationParameter(_configFileName), SectionName, dcs.Clone());
89
90 }
91
92
93
94 }
95
Reproduced in: https: //www.cnblogs.com/ycat/archive/2006/08/19/481103.html