我写了一个自定义配置部分,集合和元素来添加/修改到我的app.config。一切似乎进展顺利,它在Visual Studio中运行良好。但是,当我安装应用程序并且需要将新数据保存到自定义配置部分时,会抛出以下异常:
System.Configuration.ConfigurationErrorsException: Unable to save config to file '{path to config file}'
有趣的是,如果我以管理员模式运行应用程序,一切正常。有什么理由它只能作为管理员工作吗?
修改
我应该注意,我不认为这是一个许可问题,因为a)我们有其他应用程序可以修改他们的app.conf的<applicationSettings>
就好了,b)log4net可以编写它日志文件就好了。
自定义配置元素:
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; }
}
}
自定义配置部分:
[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);
}
}
}
自定义配置部分
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();
}
}
答案 0 :(得分:2)
我不喜欢使用System.Configuration命名空间中的方法写来配置文件。 使用System.Configuration命名空间中的方法读取值绝不是问题。
只需使用LinqToXML,至少要检查是否存在问题。
我猜测您的应用已安装在C:\Program Data
中,因此虽然可能与权限相关,但如果该假设正确,我怀疑System.Configuration.ConfigurationErrorsException: Unable to save config to file
的根本原因是lock on the file, use this code to check