私もこの必要性を解決しようとしましたが、共有したい素敵なきれいな ConsoleApplication ができました: (App.config)
表示される内容は次のとおりです。
- すべての AppSetting プロパティを読み取る方法
- 新しいプロパティを挿入する方法
- プロパティを削除する方法
- プロパティを更新する方法
楽しむ!
public void UpdateProperty(string key, string value)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;
// update SaveBeforeExit
config.AppSettings.Settings[key].Value = value;
Console.Write("...Configuration updated: key "+key+", value: "+value+"...");
//save the file
config.Save(ConfigurationSaveMode.Modified);
Console.Write("...saved Configuration...");
//relaod the section you modified
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
Console.Write("...Configuration Section refreshed...");
}
public void ReadAppSettingsProperty()
{
try
{
var section = ConfigurationManager.GetSection("applicationSettings");
// Get the AppSettings section.
NameValueCollection appSettings = ConfigurationManager.AppSettings;
// Get the AppSettings section elements.
Console.WriteLine();
Console.WriteLine("Using AppSettings property.");
Console.WriteLine("Application settings:");
if (appSettings.Count == 0)
{
Console.WriteLine("[ReadAppSettings: {0}]", "AppSettings is empty Use GetSection command first.");
}
for (int i = 0; i < appSettings.Count; i++)
{
Console.WriteLine("#{0} Key: {1} Value: {2}",
i, appSettings.GetKey(i), appSettings[i]);
}
}
catch (ConfigurationErrorsException e)
{
Console.WriteLine("[ReadAppSettings: {0}]", e.ToString());
}
}
public void updateAppSettingProperty(string key, string value)
{
// Get the application configuration file.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string sectionName = "appSettings";
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);
SaveConfigFile(config);
}
public void insertAppSettingProperty(string key, string value)
{
// Get the application configuration file.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string sectionName = "appSettings";
config.AppSettings.Settings.Add(key, value);
SaveConfigFile(config);
}
public void deleteAppSettingProperty(string key)
{
// Get the application configuration file.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(key);
SaveConfigFile(config);
}
private static void SaveConfigFile(System.Configuration.Configuration config)
{
string sectionName = "appSettings";
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section. This
// makes the new values available for reading.
ConfigurationManager.RefreshSection(sectionName);
// Get the AppSettings section.
AppSettingsSection appSettingSection =
(AppSettingsSection)config.GetSection(sectionName);
Console.WriteLine();
Console.WriteLine("Using GetSection(string).");
Console.WriteLine("AppSettings section:");
Console.WriteLine(appSettingSection.SectionInformation.GetRawXml());
}
}
構成ファイルは次のようになります。
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="aNewKey1" value="aNewValue1" />
</appSettings>
さて、このソリューションでは AppSettings に問題はありませんでした! 楽しむ... ;-) !