有没有办法从代码中写入自定义app.Config配置

时间:2014-07-31 14:08:30

标签: c# list app-config

到目前为止,我有:

<section name="PinnedPhotos" type="PhotoViewer.PinnedPhotos, PhotoViewer" allowLocation="true" allowDefinition="Everywhere"/>
<PinnedPhotos>
  <add location="Location1.jpg"/>
  <add location="Location2.jpg"/>
</PinnedPhotos>

在我的app.Config

在我的代码中我有:

 PinnedPhotos config = (PinnedPhotos)System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location).Sections["PinnedPhotos"];

但我想从代码中更新并保存config,例如添加更多要固定到现有列表的照片。有没有办法做到这一点?

修改

var isReadOnly = config.Photos.IsReadOnly();

返回False ...所以我想应该有办法写这个集合吗?

1 个答案:

答案 0 :(得分:1)

知道了: 我需要将两个方法添加到我的集合中:

    public void Add(string location)
    {
        PhotoElement pe = new PhotoElement(location);
        BaseAdd(pe);
    }
    public void Remove(string location)
    {
        BaseRemove(location);
    }

然后这只是一个打电话的案例:

config.CurrentConfiguration.Save();
相关问题