自定义配置部分只能 saved/modified 而 运行 作为管理员?
Custom Configuration Section can only be saved/modified while running as administrator?
我将自定义配置部分、集合和元素写到 add/modify 到我的 app.config。一切似乎都很顺利,并且在 运行 中完美地工作 Visual Studio。但是,当我安装应用程序并将新数据保存到自定义配置部分时,抛出以下异常:
System.Configuration.ConfigurationErrorsException: Unable to save config to file '{path to config file}'
有趣的是,如果我 运行 应用程序处于管理员模式,一切正常。有什么理由只能作为管理员使用吗?
编辑:
我应该指出,我不认为这是一个权限问题,因为 a) 我们有其他应用程序可以修改他们 app.config 的 <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();
}
}
我不喜欢使用 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 如果这个假设是正确的.
我将自定义配置部分、集合和元素写到 add/modify 到我的 app.config。一切似乎都很顺利,并且在 运行 中完美地工作 Visual Studio。但是,当我安装应用程序并将新数据保存到自定义配置部分时,抛出以下异常:
System.Configuration.ConfigurationErrorsException: Unable to save config to file '{path to config file}'
有趣的是,如果我 运行 应用程序处于管理员模式,一切正常。有什么理由只能作为管理员使用吗?
编辑:
我应该指出,我不认为这是一个权限问题,因为 a) 我们有其他应用程序可以修改他们 app.config 的 <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();
}
}
我不喜欢使用 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 如果这个假设是正确的.