【发布时间】:2016-09-01 18:31:25
【问题描述】:
我编写了一个自定义配置部分、集合和元素以添加/修改到我的 app.config。一切似乎进展顺利,它在 Visual Studio 中完美运行。但是,当我安装应用程序并且需要将新数据保存到自定义配置部分时,会引发以下异常:
System.Configuration.ConfigurationErrorsException: Unable to save config to file '{path to config file}'
有趣的是,如果我以管理员模式运行应用程序,一切正常。有什么理由只能作为管理员工作吗?
编辑:
我应该注意,我不认为这是一个权限问题,因为 a)我们有其他应用程序可以修改其 app.configs 的<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();
}
}
【问题讨论】:
-
你写信给哪里?身体在哪里?运行您的应用的用户是否有权在此处写入?
-
@mariocatch 写入 ProgramData,但我们套件中的其他应用程序可以很好地修改也存储在那里的配置文件。正在修改的
部分(适用于我们的其他应用程序)与自定义部分在权限方面是否有区别?此外,我的应用程序使用 log4net,它似乎没有将日志写入该位置的问题。 -
你的 app.config 是什么样的?只是部分声明部分
-
@mariocatch 好吧,它是一个自定义部分,所以它在第一次运行时不存在,但是当它成功创建(调试模式或以管理员身份运行)时,它看起来像这样:
<section name="AssignedColors" type="TMIAuditorTerritoryExtract.AuditorColorSection, TMIAuditorTerritoryExtract, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
标签: c# configuration app-config configuration-files