【发布时间】:2017-09-14 21:13:12
【问题描述】:
问题:用户在初始启动时设置的值不会保留在 App.config xml 中
所需功能:用户设置的值保留在 App.config 中,除非用户更改
我正在努力做到这一点,以便用户第一次运行应用程序时,他们设置了一个文件夹,他们使用该程序制作的文件将被保存在其中。我想将该字符串存储在 App.config 中,以便只需设置一次。截至目前,它将设置它并将其保存在内存中,但如果程序关闭,该值将丢失。我认为调用 .save() 方法可以解决它,但它没有。 打开表单时:
if (ConfigurationManager.AppSettings["SaveFilePath"] == null)
{
FolderBrowserDialog fbd1 = new FolderBrowserDialog();
fbd1.RootFolder = Environment.SpecialFolder.MyComputer;
fbd1.Description = "Select a folder to save to:";
fbd1.ShowNewFolderButton = true;
if (fbd1.ShowDialog() == DialogResult.OK)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("SaveFilePath", fbd1.SelectedPath);
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
}
【问题讨论】:
-
App.Config 并非旨在成为持久存储解决方案。您应该考虑以另一种方式保留这些选项,也许是 settings.json?
标签: c# xml winforms app-config