问题
I wrote a custom configuration section, collection, and element to add/modify to my app.config. All seems to be going well and it works perfectly running in Visual Studio. However, when I install the application and it comes time to save the new data to the custom config section, the following exception is thrown:
System.Configuration.ConfigurationErrorsException: Unable to save config to file '{path to config file}'
The interesting part is that if I run the application in administrator mode, everything works fine. Is there any reason it would only work as administrator?
EDIT:
I should note that I don't think its a permissons issue because a) we have other apps that modify the <applicationSettings>
of their app.configs just fine there and b) log4net is able to write its log file just fine there.
Custom Config Element:
public class AuditorColorElement : ConfigurationElement
{
public AuditorColorElement()
{
}
public AuditorColorElement(Color color, string auditor)
{
Color = color;
Auditor = auditor;
}
[ConfigurationProperty(nameof(Color), IsRequired = true, IsKey = true)]
public Color Color
{
get { return (Color)base[nameof(Color)]; }
set { this[nameof(Color)] = value; }
}
[ConfigurationProperty(nameof(Auditor), IsRequired = true)]
public string Auditor
{
get { return (string)base[nameof(Auditor)]; }
set { this[nameof(Auditor)] = value; }
}
}
Custom Config Section:
[ConfigurationCollection(typeof(AuditorColorElement))]
public class AuditorColorElementCollection : ConfigurationElementCollection, IEnumerable<AuditorColorElement>
{
internal const string PropertyName = "AuditorColors";
public AuditorColorElementCollection() : base()
{
}
public AuditorColorElementCollection(AuditorColorElementCollection collection)
{
foreach (AuditorColorElement element in collection)
{
Add(element);
}
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.AddRemoveClearMapAlternate;
}
}
protected override string ElementName
{
get
{
return PropertyName;
}
}
public AuditorColorElement this[int idx]
{
get { return (AuditorColorElement)BaseGet(idx); }
}
protected override bool IsElementName(string elementName)
{
return elementName.Equals(PropertyName,
StringComparison.InvariantCultureIgnoreCase);
}
public override bool IsReadOnly()
{
return false;
}
protected override ConfigurationElement CreateNewElement()
{
return new AuditorColorElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((AuditorColorElement)(element)).Color;
}
public void Add(AuditorColorElement element)
{
BaseAdd(element);
}
public void Clear()
{
BaseClear();
}
public void Remove(AuditorColorElement element)
{
BaseRemove(element.Color);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(Color color)
{
BaseRemove(color);
}
public object GetKey(object key)
{
return BaseGet(key);
}
public bool ContainsKey(object key)
{
return GetKey(key) != null ? true : false;
}
public new IEnumerator<AuditorColorElement> GetEnumerator()
{
foreach (var key in this.BaseGetAllKeys())
{
yield return (AuditorColorElement)BaseGet(key);
}
}
}
Custom Config Section
public class AuditorColorSection : ConfigurationSection
{
[ConfigurationProperty(nameof(AuditorColors), IsRequired = true, IsDefaultCollection = true)]
[ConfigurationCollection(typeof(AuditorColorElementCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public AuditorColorElementCollection AuditorColors
{
get { return ((AuditorColorElementCollection)(base[nameof(AuditorColors)])); }
set { base[nameof(AuditorColors)] = value; }
}
public AuditorColorSection()
{
AuditorColors = new AuditorColorElementCollection();
}
}
回答1:
I'm not a fan of using methods in the System.Configuration namespace to write to config files. Reading values using methods in the System.Configuration namespace is never a problem.
Just use LinqToXML instead, at least to check if that is the problem.
I'm guessing your app is installed in C:\Program Data
so while it could be permissions related I suspect the root cause of the System.Configuration.ConfigurationErrorsException: Unable to save config to file
is a lock on the file, use this code to check if that assumption is correct.
来源:https://stackoverflow.com/questions/37076525/custom-configuration-section-can-only-be-saved-modified-while-running-as-adminis