如果涉及运行时写配置文件则最好将配置文件分开存储,因为.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方法避免这一标志被置位。见如下代码:
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方法避免这一标志被置位。见如下代码:
1
public
class
MySettings : SerializableConfigurationSection
2 {
3 private const string _configSourceName = "MyConfigurationSource";
4 private const string SectionName = "MyConfiguration";
5
6 private static MySettings defaultInstance;
7
8 ..
9
10 //为了运行时保存配置文件的内容定义一些变量
11 private static FileConfigurationSource _fileConfigurationSource=null;
12 private static string _configFileName = string.Empty;
13
14
15
16 //配置机制会自动加载配置片断,由EnterpriseLibrary for .Net 2.0框架封装
17 public static MySettings Default
18 {
19 get
20 {
21 if (defaultInstance == null)
22 {
23 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
2 {
3 private const string _configSourceName = "MyConfigurationSource";
4 private const string SectionName = "MyConfiguration";
5
6 private static MySettings defaultInstance;
7
8 ..
9
10 //为了运行时保存配置文件的内容定义一些变量
11 private static FileConfigurationSource _fileConfigurationSource=null;
12 private static string _configFileName = string.Empty;
13
14
15
16 //配置机制会自动加载配置片断,由EnterpriseLibrary for .Net 2.0框架封装
17 public static MySettings Default
18 {
19 get
20 {
21 if (defaultInstance == null)
22 {
23 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
转载于:https://www.cnblogs.com/ycat/archive/2006/08/19/481103.html