【发布时间】:2018-07-30 19:30:30
【问题描述】:
我正在使用 C# 创建一个表单应用程序。这个应用程序应该创建某些文件,这些文件需要有一个递增的数字。因此我想我可以在 app.config 中存储一个 Counter 并在每次创建文件时递增它。
我曾经写过这段代码:
System.Configuration.Configuration _Config = null;
public Form1()
{
InitializeComponent();
_Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
private void btnNew_Click(object sender, EventArgs e)
{
int lCount;
if (int.TryParse(ConfigurationManager.AppSettings["count"], out lCount))
{
_Config.AppSettings.Settings["count"].Value = lCount++.ToString();
_Config.Save(ConfigurationSaveMode.Modified);
MessageBox.Show(ConfigurationManager.AppSettings["count"].ToString());
}
else
{
throw new Exception("Count in Config file is no int");
}
}
它不会更新值。我是否需要重新加载配置,或者无论如何在 app.config 中存储计数器值没有任何意义?
谢谢
【问题讨论】:
-
App.config 不适合。您可以使用
Settings或 SQLite 等嵌入式数据库。 -
@Crowcoder 更好地使用 SQLite 方法,以防应用程序发展并成为多用户,修改起来可能非常简单。
标签: c# config app-config