简体   繁体   English

有没有办法从代码写入自定义应用程序配置配置

[英]Is there a way to write to a Custom app.Config Configuration from code

So far I have: 到目前为止,我有:

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

in my app.Config 在我的app.Config中

and in my Code I have: 在我的代码中,我有:

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

But I would like to update and save config from code eg Adding more photos to be pinned to the existing list. 但是我想从代码中更新和保存config ,例如添加更多照片以固定到现有列表中。 Is there a way to do this? 有没有办法做到这一点?

EDIT 编辑

var isReadOnly = config.Photos.IsReadOnly();

returns False... So I guess there should be a way to write to this collection? 返回False ...所以我想应该有一种方法可以写入此集合?

Got it: I needed to add the two Methods to my Collection: 知道了:我需要将两个方法添加到我的收藏夹中:

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

and then It was just a Case of calling: 然后这只是一个调用案例:

config.CurrentConfiguration.Save();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM