Showing the changes to Source/Libraries/GSF.Core/Configuration/ConfigurationFile.cs (1 of 13 files). Show all
Unified · Split
| @@ -64,7 +64,6 @@ using System.Configuration; · 63 unmodified lines | |||
| 64 | using System.Globalization; | 64 | using System.Globalization; |
| 65 | using System.IO; | 65 | using System.IO; |
| 66 | using System.Text; | 66 | using System.Text; |
| 67 | using System.Threading; | ||
| 68 | using System.Web.Configuration; | 67 | using System.Web.Configuration; |
| 69 | using System.Web.Hosting; | 68 | using System.Web.Hosting; |
| 70 | using System.Xml; | 69 | using System.Xml; |
| @@ -177,7 +176,6 @@ namespace GSF.Configuration · 106 unmodified lines | |||
| 177 | #region [ Members ] | 176 | #region [ Members ] |
| 178 | 177 | ||
| 179 | // Nested Types | 178 | // Nested Types |
| 180 | |||
| 181 | internal class UserConfigurationFile | 179 | 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 | else | 255 | |
| 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 | #endregion | 279 | #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 | else | 295 | 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 save | 387 | // 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 in | 521 | // 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.Net | 546 | // 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 | else | 551 | else |
| 536 | { | 552 | { |