Permalink
Browse files

[System.Configuration]: SaveMode and UserLevel.

* Configuration.Save() and SaveAs() now honors the
  `ConfigurationSaveMode' argument.

* Check whether the file has been modified prior to saving.

* Fix ConfigurationUserLevel inheritance.

* Implement ConfigurationElement.IsModified().

* New ConfigurationSaveTest.cs test:

  - load a custom section using different parent configuration
    files using ExeConfigurationFileMap and different
    ConfigurationUserLevel values.

  - save configuration using different ConfigurationSaveMode
    arguments and different inherited parent configurations.

* ExeConfigurationFileMapTest.cs: Add error checks for missing
  required filenames.

* New TestUtil.cs helper class:

  - RunWithTempFile() and RunWithTempFiles() helper functions.
  - DotNetVersion, ThisDllName and ThisConfigFileName properties,
    moving all the #ifdef's into one central place.
  • Loading branch information...
baulig committed Dec 21, 2012
1 parent 9addbed commit 0b57238424265deb24a4c047a80da23501f9f240
@@ -83,6 +83,9 @@ protected void ThrowException (string text, XmlReader reader)
public abstract void WriteData (Configuration config, XmlWriter writer, ConfigurationSaveMode mode);
internal abstract void Merge (ConfigInfo data);
+
+ internal abstract bool HasValues (Configuration config, ConfigurationSaveMode mode);
+ internal abstract void ResetModified (Configuration config);
}
}
@@ -391,6 +391,11 @@ public void Save (ConfigurationSaveMode mode)
public void Save (ConfigurationSaveMode mode, bool forceUpdateAll)
{
+ if (!forceUpdateAll && (mode != ConfigurationSaveMode.Full) && !HasValues (mode)) {
+ ResetModified ();
+ return;
+ }
+
ConfigurationSaveEventHandler saveStart = SaveStart;
ConfigurationSaveEventHandler saveEnd = SaveEnd;
@@ -427,6 +432,11 @@ public void SaveAs (string filename, ConfigurationSaveMode mode)
[MonoInternalNote ("Detect if file has changed")]
public void SaveAs (string filename, ConfigurationSaveMode mode, bool forceUpdateAll)
{
+ if (!forceUpdateAll && (mode != ConfigurationSaveMode.Full) && !HasValues (mode)) {
+ ResetModified ();
+ return;
+ }
+
string dir = Path.GetDirectoryName (Path.GetFullPath (filename));
if (!Directory.Exists (dir))
Directory.CreateDirectory (dir);
@@ -464,6 +474,7 @@ void Save (Stream stream, ConfigurationSaveMode mode, bool forceUpdateAll)
SaveData (tw, mode, forceUpdateAll);
tw.WriteEndElement ();
+ ResetModified ();
}
finally {
tw.Flush ();
@@ -475,6 +486,29 @@ void SaveData (XmlTextWriter tw, ConfigurationSaveMode mode, bool forceUpdateAll
{
rootGroup.WriteRootData (tw, this, mode);
}
+
+ bool HasValues (ConfigurationSaveMode mode)
+ {
+ foreach (ConfigurationLocation loc in Locations) {
+ if (loc.OpenedConfiguration == null)
+ continue;
+ if (loc.OpenedConfiguration.HasValues (mode))
+ return true;
+ }
+
+ return rootGroup.HasValues (this, mode);
+ }
+
+ void ResetModified ()
+ {
+ foreach (ConfigurationLocation loc in Locations) {
+ if (loc.OpenedConfiguration == null)
+ continue;
+ loc.OpenedConfiguration.ResetModified ();
+ }
+
+ rootGroup.ResetModified (this);
+ }
bool Load ()
{
@@ -493,6 +527,7 @@ bool Load ()
using (XmlTextReader reader = new ConfigXmlTextReader (stream, streamName)) {
ReadConfigFile (reader, streamName);
}
+ ResetModified ();
return true;
}
Oops, something went wrong.

0 comments on commit 0b57238

Please sign in to comment.