自定义配置部分只能在以管理员身份运行时保存/修改? [英] Custom Configuration Section can only be saved/modified while running as administrator?
问题描述
我写了一个自定义配置节,集合和元素来添加/修改我的app.config。所有似乎都很好,它的工作完美运行在Visual Studio。但是,当我安装应用程序,它得到时间保存新的数据到自定义配置部分,以下异常抛出:
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?
编辑:
我应该注意,我不认为它的permissons问题,因为我们有其他应用程序修改< applicationSettings>
他们的app.configs只是罚款和b )
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.
自定义配置元素:
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();
}
}
推荐答案
I不是使用System.Configuration命名空间中的方法写来配置文件的粉丝。 使用System.Configuration命名空间中的方法读取值从不是问题。
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.
只需使用LinqToXML,至少检查是否问题。
Just use LinqToXML instead, at least to check if that is the problem.
我猜你的应用程序安装在 C:\Program Data
权限相关我怀疑 System.Configuration.ConfigurationErrorsException:无法保存配置文件
的根本原因是锁定文件,使用此代码检查正确。
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.
这篇关于自定义配置部分只能在以管理员身份运行时保存/修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!