私はthis Stackoverflow topicに従っています。すべてが読み取り側で動作します。私はセクションの内容の辞書に変換するコレクションを取得します。これを他のプロジェクトに公開します。問題は、変更されたセクションエントリを保存しようとしたときに発生します。外部プロジェクトの1つがキー/値ペアを送信し、それを自分のセクションに保存する必要があります。ここに私のApp.Configがあります設定ファイルの変更を保存できません
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="fileEnvironmentSection" type="MyApp.Infrastructure.Configuration.FileEnvironmentSection, MyApp.Infrastructure"/>
</configSections>
<fileEnvironmentSection>
<fileEnvironment>
<add key="TestEntry1" value="A nice value"/>
<add key="TestEntry2" value="Another value"/>
</fileEnvironment>
</fileEnvironmentSection>
</configuration>
ここでは、configセクションと対話するのに必要な3つのタイプのレイヤーを示します。ここで
using System.Configuration;
namespace SlamDunk.Infrastructure.Configuration {
public class FileEnvironmentSection : ConfigurationSection {
[ConfigurationProperty("fileEnvironment", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(FileEnvironmentCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public FileEnvironmentCollection FileEnvironmentList {
get { return (FileEnvironmentCollection)base["fileEnvironment"]; }
}
}
}
using System.Configuration;
namespace SlamDunk.Infrastructure.Configuration {
public class FileEnvironmentCollection : ConfigurationElementCollection {
public FileEnvironmentElement this[int index] {
get { return (FileEnvironmentElement)BaseGet(index); }
set {
if(BaseGet(index) != null) BaseRemoveAt(index);
BaseAdd(index, value);
}
}
public void Add(FileEnvironmentElement fileEnvironmentElement) { BaseAdd(fileEnvironmentElement); }
public void Clear() { BaseClear(); }
protected override ConfigurationElement CreateNewElement() { return new FileEnvironmentElement(); }
protected override object GetElementKey(ConfigurationElement element) { return ((FileEnvironmentElement)element).Key; }
public void Remove(FileEnvironmentElement fileEnvironmentElement) { BaseRemove(fileEnvironmentElement.Key); }
public void Remove(string key) { BaseRemove(key); }
public void RemoveAt(int index) { BaseRemoveAt(index); }
}
}
using System.Configuration;
namespace SlamDunk.Infrastructure.Configuration {
public class FileEnvironmentElement : ConfigurationElement {
private const string appConfigDefaultString = "missing";
private const string _appConfigNameKey = "key";
private const string _appConfigNameValue = "value";
public FileEnvironmentElement() { }
public FileEnvironmentElement(string key, string value) {
Key = key;
Value = value;
}
[ConfigurationProperty(_appConfigNameKey, DefaultValue = appConfigDefaultString, IsRequired = true, IsKey = true)]
public string Key {
get { return (string)this[_appConfigNameKey]; }
set { this[_appConfigNameKey] = value; }
}
[ConfigurationProperty(_appConfigNameValue, DefaultValue = appConfigDefaultString, IsRequired = true, IsKey = false)]
public string Value {
get { return (string)this[_appConfigNameValue]; }
set { this[_appConfigNameValue] = value; }
}
}
}
は、私は、キー/値の変更を保存するために使用しているコードです。
var appConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var fileEnvironmentSection = appConfiguration.GetSection("fileEnvironmentSection") as FileEnvironmentSection;
var fileEnvironmentList = fileEnvironmentSection.FileEnvironmentList;
fileEnvironmentList.Remove(key);
var element = new FileEnvironmentElement(key, value);
fileEnvironmentList.Add(element);
//appConfiguration.Save(ConfigurationSaveMode.Modified);
//fileEnvironmentSection.CurrentConfiguration.Save(ConfigurationSaveMode.Modified);
fileEnvironmentList.CurrentConfiguration.Save(ConfigurationSaveMode.Modified);
確認したところ、変更内容が期待どおりに一覧に表示されます。私は、2つのコメント付きセーブコールと最後のコールを試しました。私は、ConfigurationManagerのキャッシュの問題を避けるためにFileEnvironmentSectionの新しいインスタンスを取得していると思います。各テストの実行後、私はMyApp.exe.configを調べ、何も変更されていません。私は何かを逃しているし、何かを理解する助けを使用することができます。ありがとう。
まあ、変更が見つかりました。彼らはmyApp.Console.vshost.exe.Configに入ります。 Visual Studioの外でmyApp.exeを実行すると、myApp.exe.configに変更が加えられます。ここでは[Stackoverflowのトピック](http://stackoverflow.com/questions/8840904/app-config-are-not-saving-the-values)vshost.exeを見に私を導いた。誤ったアラームのために申し訳ありません。 – JimBoone