History

Added support for GSF configuration files on Mono

ritchiecarroll committed 12 years ago

Commit 2bbd6cd1 parent ef6432e13 files · +615 −136Browse files

Showing the changes to Source/Libraries/GSF.Core/Configuration (4 of 13 files). Show all

Unified · Split

4 files
  1. Source/Libraries/GSF.Core/Configuration/CategorizedSettingsElement.cs +17 −0
  2. Source/Libraries/GSF.Core/Configuration/CategorizedSettingsElementCollection.cs +18 −6
  3. Source/Libraries/GSF.Core/Configuration/CategorizedSettingsSection.cs +174 −14
  4. Source/Libraries/GSF.Core/Configuration/ConfigurationFile.cs +49 −33
Source/Libraries/GSF.Core/Configuration/CategorizedSettingsElement.cs+17−0
@@ -170,6 +170,7 @@ namespace GSF.Configuration · 169 unmodified lines
170 set170 set
171 {171 {
172 base["name"] = value;172 base["name"] = value;
173 Modified = true;
173 }174 }
174 }175 }
175 176
@@ -229,6 +230,8 @@ namespace GSF.Configuration · 53 unmodified lines
229 else230 else
230 // Setting is user specific so update setting in user settings store.231 // Setting is user specific so update setting in user settings store.
231 Category.Section.File.UserSettings.Write(Category.Name, Name, value);232 Category.Section.File.UserSettings.Write(Category.Name, Name, value);
233
234 Modified = true;
232 }235 }
233 }236 }
234 237
@@ -250,6 +253,7 @@ namespace GSF.Configuration · 15 unmodified lines
250 return;253 return;
251 254
252 base["description"] = value;255 base["description"] = value;
256 Modified = true;
253 }257 }
254 }258 }
255 259
@@ -271,6 +275,7 @@ namespace GSF.Configuration · 15 unmodified lines
271 return;275 return;
272 276
273 base["encrypted"] = value;277 base["encrypted"] = value;
278 Modified = true;
274 }279 }
275 }280 }
276 281
@@ -291,6 +296,16 @@ namespace GSF.Configuration · 14 unmodified lines
291 return;296 return;
292 297
293 base["scope"] = value;298 base["scope"] = value;
299 Modified = true;
300 }
301 }
302
303 internal bool Modified
304 {
305 set
306 {
307 if ((object)m_category != null)
308 m_category.Modified = value;
294 }309 }
295 }310 }
296 311
@@ -311,6 +326,7 @@ namespace GSF.Configuration · 14 unmodified lines
311 string decryptedValue = Value;326 string decryptedValue = Value;
312 m_cryptoKey = cryptoKey;327 m_cryptoKey = cryptoKey;
313 Value = decryptedValue;328 Value = decryptedValue;
329 Modified = true;
314 }330 }
315 }331 }
316 332
@@ -357,6 +373,7 @@ namespace GSF.Configuration · 40 unmodified lines
357 this.Encrypted = encrypted;373 this.Encrypted = encrypted;
358 this.Value = Common.TypeConvertToString(value, m_category.Section.File.Culture);374 this.Value = Common.TypeConvertToString(value, m_category.Section.File.Culture);
359 this.Description = description;375 this.Description = description;
376 Modified = true;
360 }377 }
361 378
362 /// <summary>379 /// <summary>
Source/Libraries/GSF.Core/Configuration/CategorizedSettingsElementCollection.cs+18−6
@@ -111,6 +111,7 @@ namespace GSF.Configuration · 110 unmodified lines
111 base.BaseRemoveAt(index);111 base.BaseRemoveAt(index);
112 112
113 base.BaseAdd(index, value);113 base.BaseAdd(index, value);
114 Modified = true;
114 }115 }
115 }116 }
116 117
@@ -137,23 +138,29 @@ namespace GSF.Configuration · 20 unmodified lines
137 {138 {
138 get139 get
139 {140 {
141 // Add setting since it's not there
140 if (ensureExistance && (object)base.BaseGet(name) == null)142 if (ensureExistance && (object)base.BaseGet(name) == null)
141 {
142 // Add setting since it's not there.
143 Add(name, string.Empty);143 Add(name, string.Empty);
144 }
145 144
146 CategorizedSettingsElement setting = (CategorizedSettingsElement)base.BaseGet(name);145 CategorizedSettingsElement setting = (CategorizedSettingsElement)base.BaseGet(name);
146
147 // Set the crypto key for the setting
147 if ((object)setting != null)148 if ((object)setting != null)
148 {
149 // Set the crypto key for the setting.
150 setting.SetCryptoKey(m_cryptoKey);149 setting.SetCryptoKey(m_cryptoKey);
151 }
152 150
153 return setting;151 return setting;
154 }152 }
155 }153 }
156 154
155 internal bool Modified
156 {
157 set
158 {
159 if ((object)m_section != null)
160 m_section.Modified = value;
161 }
162 }
163
157 #endregion164 #endregion
158 165
159 #region [ Methods ]166 #region [ Methods ]
@@ -242,6 +249,7 @@ namespace GSF.Configuration · 82 unmodified lines
242 setting.Category = this;249 setting.Category = this;
243 setting.SetCryptoKey(m_cryptoKey);250 setting.SetCryptoKey(m_cryptoKey);
244 base.BaseAdd(setting);251 base.BaseAdd(setting);
252 Modified = true;
245 }253 }
246 }254 }
247 255
@@ -252,6 +260,7 @@ namespace GSF.Configuration · 4 unmodified lines
252 public void Remove(string name)260 public void Remove(string name)
253 {261 {
254 base.BaseRemove(name);262 base.BaseRemove(name);
263 Modified = true;
255 }264 }
256 265
257 /// <summary>266 /// <summary>
@@ -263,6 +272,7 @@ namespace GSF.Configuration · 5 unmodified lines
263 if (base.BaseIndexOf(setting) >= 0)272 if (base.BaseIndexOf(setting) >= 0)
264 {273 {
265 Remove(setting.Name);274 Remove(setting.Name);
275 Modified = true;
266 }276 }
267 }277 }
268 278
@@ -273,6 +283,7 @@ namespace GSF.Configuration · 4 unmodified lines
273 public void RemoveAt(int index)283 public void RemoveAt(int index)
274 {284 {
275 base.BaseRemoveAt(index);285 base.BaseRemoveAt(index);
286 Modified = true;
276 }287 }
277 288
278 /// <summary>289 /// <summary>
@@ -281,6 +292,7 @@ namespace GSF.Configuration · 2 unmodified lines
281 public void Clear()292 public void Clear()
282 {293 {
283 base.BaseClear();294 base.BaseClear();
295 Modified = true;
284 }296 }
285 297
286 /// <summary>298 /// <summary>
Source/Libraries/GSF.Core/Configuration/CategorizedSettingsSection.cs+174−14
@@ -38,10 +38,14 @@ · 37 unmodified lines
38//******************************************************************************************************38//******************************************************************************************************
39 39
40using System;40using System;
41using System.Collections.Generic;
41using System.Configuration;42using System.Configuration;
42using System.IO;43using System.IO;
44using System.Text;
43using System.Xml;45using System.Xml;
46using GSF.Collections;
44using GSF.IO;47using GSF.IO;
48using GSF.Xml;
45 49
46namespace GSF.Configuration50namespace GSF.Configuration
47{51{
@@ -59,9 +63,19 @@ namespace GSF.Configuration · 11 unmodified lines
59 // Fields63 // Fields
60 private string m_cryptoKey;64 private string m_cryptoKey;
61 private ConfigurationFile m_file;65 private ConfigurationFile m_file;
66 private readonly Dictionary<string, CategorizedSettingsElementCollection> m_sections;
62 67
63 #endregion68 #endregion
64 69
70 /// <summary>
71 /// Creates a new <see cref="CategorizedSettingsSection"/>.
72 /// </summary>
73 public CategorizedSettingsSection()
74 {
75 if (Common.IsMono)
76 m_sections = new Dictionary<string, CategorizedSettingsElementCollection>();
77 }
78
65 #region [ Properties ]79 #region [ Properties ]
66 80
67 /// <summary>81 /// <summary>
@@ -99,18 +113,42 @@ namespace GSF.Configuration · 31 unmodified lines
99 // Make the first letter of category name lower case, if not already.113 // Make the first letter of category name lower case, if not already.
100 char[] nameChars = name.ToCharArray();114 char[] nameChars = name.ToCharArray();
101 nameChars[0] = char.ToLower(nameChars[0]);115 nameChars[0] = char.ToLower(nameChars[0]);
116
102 // Do not allow spaces in the name so that underlying .Net configuration API does not break.117 // Do not allow spaces in the name so that underlying .Net configuration API does not break.
103 name = (new string(nameChars)).RemoveWhiteSpace();118 name = (new string(nameChars)).RemoveWhiteSpace();
104 ConfigurationProperty configProperty = new ConfigurationProperty(name, typeof(CategorizedSettingsElementCollection));
105 119
106 base.Properties.Add(configProperty);120 ConfigurationProperty configProperty = new ConfigurationProperty(name, typeof(CategorizedSettingsElementCollection));
107 CategorizedSettingsElementCollection settingsCategory = null;121 CategorizedSettingsElementCollection settingsCategory = null;
108 if ((object)base[configProperty] != null)122
123 if ((object)m_sections == null)
109 {124 {
110 settingsCategory = (CategorizedSettingsElementCollection)base[configProperty];125 base.Properties.Add(configProperty);
111 settingsCategory.Name = name;126
112 settingsCategory.Section = this;127 if ((object)base[configProperty] != null)
113 settingsCategory.SetCryptoKey(m_cryptoKey);128 {
129 settingsCategory = (CategorizedSettingsElementCollection)base[configProperty];
130 settingsCategory.Name = name;
131 settingsCategory.Section = this;
132 settingsCategory.SetCryptoKey(m_cryptoKey);
133 }
134 }
135 else
136 {
137 settingsCategory = m_sections.GetOrAdd(name, sectionName =>
138 {
139 base.Properties.Add(configProperty);
140
141 CategorizedSettingsElementCollection settings = new CategorizedSettingsElementCollection
142 {
143 Name = sectionName,
144 Section = this,
145 };
146
147 settings.SetCryptoKey(m_cryptoKey);
148 Modified = true;
149
150 return settings;
151 });
114 }152 }
115 153
116 return settingsCategory;154 return settingsCategory;
@@ -129,6 +167,15 @@ namespace GSF.Configuration · 12 unmodified lines
129 }167 }
130 }168 }
131 169
170 internal bool Modified
171 {
172 set
173 {
174 if ((object)m_sections != null && (object)m_file != null && value)
175 m_file.m_forceSave = true;
176 }
177 }
178
132 #endregion179 #endregion
133 180
134 #region [ Methods ]181 #region [ Methods ]
@@ -159,7 +206,10 @@ namespace GSF.Configuration · 24 unmodified lines
159 }206 }
160 207
161 // Remove category from property bag.208 // Remove category from property bag.
162 base.Properties.Remove(settingsCategory.Name);209 if ((object)m_sections == null)
210 base.Properties.Remove(settingsCategory.Name);
211 else
212 m_sections.Remove(settingsCategory.Name);
163 }213 }
164 214
165 /// <summary>215 /// <summary>
@@ -181,19 +231,129 @@ namespace GSF.Configuration · 15 unmodified lines
181 // when the category elements are being deserialized.231 // when the category elements are being deserialized.
182 if ((object)configSection.DocumentElement != null)232 if ((object)configSection.DocumentElement != null)
183 {233 {
184 XmlNodeList nodeList = configSection.DocumentElement.SelectNodes("*");234 XmlNodeList categories = configSection.DocumentElement.SelectNodes("*");
185 235
186 if ((object)nodeList != null)236 if ((object)categories != null)
187 {237 {
188 foreach (XmlNode category in nodeList)238 foreach (XmlNode category in categories)
189 base.Properties.Add(new ConfigurationProperty(category.Name, typeof(CategorizedSettingsElementCollection)));239 {
240 ConfigurationProperty configProperty = new ConfigurationProperty(category.Name, typeof(CategorizedSettingsElementCollection));
241
242 base.Properties.Add(configProperty);
243
244 if ((object)m_sections != null)
245 {
246 CategorizedSettingsElementCollection settingsCategory = new CategorizedSettingsElementCollection
247 {
248 Name = category.Name,
249 Section = this,
250 };
251
252 settingsCategory.SetCryptoKey(m_cryptoKey);
253 m_sections.Add(category.Name, settingsCategory);
254
255 // Read all elements within this category section
256 XmlNodeList elements = category.SelectNodes("*");
257 SettingScope scope;
258
259 if ((object)elements != null)
260 {
261 foreach (XmlNode element in elements)
262 {
263 CategorizedSettingsElement categorySetting = new CategorizedSettingsElement(settingsCategory);
264
265 categorySetting.Name = element.GetAttributeValue("name");
266 categorySetting.Value = element.GetAttributeValue("value");
267 categorySetting.Description = element.GetAttributeValue("description") ?? "";
268 categorySetting.Encrypted = element.GetAttributeValue("encrypted").ToNonNullNorWhiteSpace("false").ParseBoolean();
269
270 if (Enum.TryParse(element.GetAttributeValue("scope").ToNonNullNorWhiteSpace("Application"), out scope))
271 categorySetting.Scope = scope;
272 else
273 categorySetting.Scope = SettingScope.Application;
274
275 settingsCategory.Add(categorySetting);
276 }
277 }
278 }
279 }
190 }280 }
191 }281 }
192 282
193 configSectionStream.Seek(0, SeekOrigin.Begin);283 if ((object)m_sections == null)
284 {
285 configSectionStream.Seek(0, SeekOrigin.Begin);
286 base.DeserializeSection(XmlReader.Create(configSectionStream));
287 }
288 }
289 }
290
291 /// <summary>
292 /// Creates an XML string containing an unmerged view of the <see cref="ConfigurationSection"/> object as a single section to write to a file.
293 /// </summary>
294 /// <returns>
295 /// An XML string containing an unmerged view of the <see cref="ConfigurationSection"/> object.
296 /// </returns>
297 /// <param name="parentElement">The <see cref="ConfigurationElement"/> instance to use as the parent when performing the un-merge.</param>
298 /// <param name="name">The name of the section to create.</param>
299 /// <param name="saveMode">The <see cref="ConfigurationSaveMode"/> instance to use when writing to a string.</param>
300 protected override string SerializeSection(ConfigurationElement parentElement, string name, ConfigurationSaveMode saveMode)
301 {
302 if ((object)m_sections != null)
303 {
304 const string tempRoot = "__tempRoot__";
305
306 using (BlockAllocatedMemoryStream stream = new BlockAllocatedMemoryStream())
307 {
308 XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
309
310 writer.Indentation = 2;
311 writer.Formatting = Formatting.Indented;
312
313 // Add a temporary root so that indentation is at the desired level
314 writer.WriteStartElement(tempRoot);
315 writer.WriteStartElement(name);
194 316
195 base.DeserializeSection(XmlReader.Create(configSectionStream));317 foreach (string section in m_sections.Keys)
318 {
319 CategorizedSettingsElementCollection categorySettings = this[section];
320
321 // Add category section
322 writer.WriteStartElement(section);
323
324 // Write each category value
325 foreach (CategorizedSettingsElement categorySetting in categorySettings)
326 {
327 // <add name="TestConfigParam" value="-1.0" description="Parameter description." encrypted="false" scope="User"/>
328 writer.WriteStartElement("add");
329
330 writer.WriteAttributeString("name", categorySetting.Name);
331 writer.WriteAttributeString("value", categorySetting.Value);
332 writer.WriteAttributeString("description", categorySetting.Description ?? "");
333 writer.WriteAttributeString("encrypted", categorySetting.Encrypted.ToString());
334
335 if (categorySetting.Scope == SettingScope.User)
336 writer.WriteAttributeString("scope", categorySetting.Scope.ToString());
337
338 writer.WriteEndElement();
339 }
340
341 writer.WriteEndElement();
342 }
343
344 writer.WriteEndElement();
345 writer.WriteEndElement();
346
347 writer.Flush();
348
349 string settings = Encoding.UTF8.GetString(stream.ToArray());
350
351 // Remove temporary root
352 return settings.Replace(string.Format("<{0}>", tempRoot), "").Replace(string.Format("</{0}>", tempRoot), "");
353 }
196 }354 }
355
356 return base.SerializeSection(parentElement, name, ConfigurationSaveMode.Full);
197 }357 }
198 358
199 #endregion359 #endregion
Source/Libraries/GSF.Core/Configuration/ConfigurationFile.cs+49−33
@@ -64,7 +64,6 @@ using System.Configuration; · 63 unmodified lines
64using System.Globalization;64using System.Globalization;
65using System.IO;65using System.IO;
66using System.Text;66using System.Text;
67using System.Threading;
68using System.Web.Configuration;67using System.Web.Configuration;
69using System.Web.Hosting;68using System.Web.Hosting;
70using System.Xml;69using System.Xml;
@@ -177,7 +176,6 @@ namespace GSF.Configuration · 106 unmodified lines
177 #region [ Members ]176 #region [ Members ]
178 177
179 // Nested Types178 // Nested Types
180
181 internal class UserConfigurationFile179 internal class UserConfigurationFile
182 {180 {
183 private readonly string m_fileName;181 private readonly string m_fileName;
@@ -229,6 +227,7 @@ namespace GSF.Configuration · 45 unmodified lines
229 public void Write(string category, string name, string value)227 public void Write(string category, string name, string value)
230 {228 {
231 XmlNode node = GetSetting(category, name);229 XmlNode node = GetSetting(category, name);
230
232 if ((object)node != null)231 if ((object)node != null)
233 {232 {
234 // Setting exists so update it.233 // Setting exists so update it.
@@ -242,18 +241,20 @@ namespace GSF.Configuration · 7 unmodified lines
242 setting.SetAttributeValue("value", value);241 setting.SetAttributeValue("value", value);
243 m_settings.GetXmlNode(category).AppendChild(setting);242 m_settings.GetXmlNode(category).AppendChild(setting);
244 }243 }
244
245 m_settingsModified = true;245 m_settingsModified = true;
246 }246 }
247 247
248 public string Read(string category, string name, string defaultValue)248 public string Read(string category, string name, string defaultValue)
249 {249 {
250 XmlNode node = GetSetting(category, name);250 XmlNode node = GetSetting(category, name);
251 if ((object)node != null)251
252 // Setting exists so return its value.252 // If setting exists, return its value
253 if ((object)node != null && (object)node.Attributes != null)
253 return node.Attributes["value"].Value;254 return node.Attributes["value"].Value;
254 else255
255 // Setting doesn't exist so return the default.256 // Setting doesn't exist so return the default
256 return defaultValue;257 return defaultValue;
257 }258 }
258 259
259 public XmlNode GetSetting(string category, string name)260 public XmlNode GetSetting(string category, string name)
@@ -272,7 +273,8 @@ namespace GSF.Configuration · 12 unmodified lines
272 private System.Configuration.Configuration m_configuration;273 private System.Configuration.Configuration m_configuration;
273 private UserConfigurationFile m_userConfiguration;274 private UserConfigurationFile m_userConfiguration;
274 private volatile ConfigurationSaveMode m_saveMode;275 private volatile ConfigurationSaveMode m_saveMode;
275 private LongSynchronizedOperation m_saveOperation;276 private readonly LongSynchronizedOperation m_saveOperation;
277 internal bool m_forceSave;
276 278
277 #endregion279 #endregion
278 280
@@ -288,7 +290,7 @@ namespace GSF.Configuration · 9 unmodified lines
288 290
289 m_configuration = GetConfiguration(configFilePath);291 m_configuration = GetConfiguration(configFilePath);
290 292
291 if (m_configuration.HasFile)293 if (m_configuration.HasFile && File.Exists(m_configuration.FilePath))
292 ValidateConfigurationFile(m_configuration.FilePath);294 ValidateConfigurationFile(m_configuration.FilePath);
293 else295 else
294 CreateConfigurationFile(m_configuration.FilePath);296 CreateConfigurationFile(m_configuration.FilePath);
@@ -364,7 +366,7 @@ namespace GSF.Configuration · 69 unmodified lines
364 /// </summary>366 /// </summary>
365 public void Save()367 public void Save()
366 {368 {
367 Save(ConfigurationSaveMode.Modified);369 Save(m_forceSave ? ConfigurationSaveMode.Full : ConfigurationSaveMode.Modified);
368 }370 }
369 371
370 /// <summary>372 /// <summary>
@@ -373,8 +375,7 @@ namespace GSF.Configuration · 2 unmodified lines
373 /// <param name="saveMode">One of the <see cref="ConfigurationSaveMode"/> values.</param>375 /// <param name="saveMode">One of the <see cref="ConfigurationSaveMode"/> values.</param>
374 public void Save(ConfigurationSaveMode saveMode)376 public void Save(ConfigurationSaveMode saveMode)
375 {377 {
376 if (m_saveMode == ConfigurationSaveMode.Minimal || saveMode == ConfigurationSaveMode.Full)378 m_saveMode = saveMode;
377 m_saveMode = saveMode;
378 379
379 // Since the static open method always returns the same instance for the same configuration files,380 // Since the static open method always returns the same instance for the same configuration files,
380 // synchronizing the instance will sync all saves.381 // synchronizing the instance will sync all saves.
@@ -385,9 +386,10 @@ namespace GSF.Configuration · 4 unmodified lines
385 {386 {
386 // Perform configuration save387 // Perform configuration save
387 ConfigurationSaveMode saveMode = m_saveMode;388 ConfigurationSaveMode saveMode = m_saveMode;
388 m_configuration.Save(saveMode);389 m_configuration.Save(saveMode, m_forceSave);
389 m_userConfiguration.Save(saveMode == ConfigurationSaveMode.Full);390 m_userConfiguration.Save(saveMode == ConfigurationSaveMode.Full);
390 m_saveMode = ConfigurationSaveMode.Minimal;391 m_saveMode = ConfigurationSaveMode.Minimal;
392 m_forceSave = false;
391 }393 }
392 394
393 /// <summary>395 /// <summary>
@@ -436,7 +438,7 @@ namespace GSF.Configuration · 42 unmodified lines
436 private System.Configuration.Configuration GetConfiguration(string configFilePath)438 private System.Configuration.Configuration GetConfiguration(string configFilePath)
437 {439 {
438 ApplicationType appType = Common.GetApplicationType();440 ApplicationType appType = Common.GetApplicationType();
439 System.Configuration.Configuration configuration = null;441 System.Configuration.Configuration configuration;
440 442
441 if ((object)configFilePath != null)443 if ((object)configFilePath != null)
442 {444 {
@@ -475,13 +477,15 @@ namespace GSF.Configuration · 32 unmodified lines
475 if (!string.IsNullOrEmpty(configFilePath))477 if (!string.IsNullOrEmpty(configFilePath))
476 {478 {
477 XmlTextWriter configFileWriter = new XmlTextWriter(configFilePath, Encoding.UTF8);479 XmlTextWriter configFileWriter = new XmlTextWriter(configFilePath, Encoding.UTF8);
478 configFileWriter.Indentation = 4;480 configFileWriter.Indentation = 2;
479 configFileWriter.Formatting = Formatting.Indented;481 configFileWriter.Formatting = Formatting.Indented;
482
480 // Populates the very basic information required in a config file.483 // Populates the very basic information required in a config file.
481 configFileWriter.WriteStartDocument();484 configFileWriter.WriteStartDocument();
482 configFileWriter.WriteStartElement("configuration");485 configFileWriter.WriteStartElement("configuration");
483 configFileWriter.WriteEndElement();486 configFileWriter.WriteEndElement();
484 configFileWriter.WriteEndDocument();487 configFileWriter.WriteEndDocument();
488
485 // Closes the config file.489 // Closes the config file.
486 configFileWriter.Close();490 configFileWriter.Close();
487 491
@@ -505,32 +509,44 @@ namespace GSF.Configuration · 17 unmodified lines
505 // Make sure that the config file has the necessary section information under <customSections />509 // Make sure that the config file has the necessary section information under <customSections />
506 // that is required by the .Net configuration API to process our custom <categorizedSettings />510 // that is required by the .Net configuration API to process our custom <categorizedSettings />
507 // section. The configuration API will raise an exception if it doesn't find this section.511 // section. The configuration API will raise an exception if it doesn't find this section.
508 if (configFile.DocumentElement.SelectNodes("configSections").Count == 0)512 if ((object)configFile.DocumentElement != null)
509 {513 {
510 // Adds a <configSections> node, if one is not present.514 XmlNodeList configSections = configFile.DocumentElement.SelectNodes("configSections");
511 configFile.DocumentElement.InsertBefore(configFile.CreateElement("configSections"), configFile.DocumentElement.FirstChild);
512 515
513 configFileModified = true;516 if ((object)configSections != null && configSections.Count == 0)
514 }517 {
515 XmlNode configSectionsNode = configFile.DocumentElement.SelectSingleNode("configSections");518 // Add a <configSections> node, if one is not present.
516 if (configSectionsNode.SelectNodes("section[@name = \'" + CustomSectionName + "\']").Count == 0)519 if ((object)configFile.DocumentElement != null)
517 {520 {
518 // Adds the <section> node that specifies the DLL that handles the <categorizedSettings> node in521 // ReSharper disable once PossibleNullReferenceException
519 // the config file, if one is not present.522 configFile.DocumentElement.InsertBefore(configFile.CreateElement("configSections"), configFile.DocumentElement.FirstChild);
520 XmlNode node = configFile.CreateElement("section");523 configFileModified = true;
521 node.SetAttributeValue("name", CustomSectionName);524 }
522 node.SetAttributeValue("type", CustomSectionType);525 }
523 configSectionsNode.AppendChild(node);526
524 527 XmlNode configSectionsNode = configFile.DocumentElement.SelectSingleNode("configSections");
525 configFileModified = true;528
529 if ((object)configSectionsNode != null)
530 {
531 XmlNodeList section = configSectionsNode.SelectNodes("section[@name = \'" + CustomSectionName + "\']");
532
533 if ((object)section != null && section.Count == 0)
534 {
535 // Adds the <section> node that specifies the DLL that handles the <categorizedSettings> node in
536 // the config file, if one is not present.
537 XmlNode node = configFile.CreateElement("section");
538 node.SetAttributeValue("name", CustomSectionName);
539 node.SetAttributeValue("type", CustomSectionType);
540 configSectionsNode.AppendChild(node);
541 configFileModified = true;
542 }
543 }
526 }544 }
527 545
528 // 11/14/2006 - PCP: We'll save the config file only it was modified. This will prevent ASP.Net546 // 11/14/2006 - PCP: We'll save the config file only it was modified. This will prevent ASP.Net
529 // web sites from restarting every time a configuration element is accessed.547 // web sites from restarting every time a configuration element is accessed.
530 if (configFileModified)548 if (configFileModified)
531 {
532 configFile.Save(configFilePath);549 configFile.Save(configFilePath);
533 }
534 }550 }
535 else551 else
536 {552 {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t