C# app.config 保存和读取例子
阅读原文时间:2023年07月08日阅读:1

保存:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
if (config.AppSettings.Settings.AllKeys.Contains("key"))
{
config.AppSettings.Settings["key"].Value = fName;
}
else
{
config.AppSettings.Settings.Add(new KeyValueConfigurationElement("key", fName));
}
config.Save();

读取:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
if (config.AppSettings.Settings.AllKeys.Contains("key"))
{
var fileName = config.AppSettings.Settings["key"].Value;
textBox1.Text = fileName;
}