我的Windows应用程序由各种设置组成,这些设置主要包括数字形式的数据(十进制和十六进制)以及一些comboBox。
当用户在这些框中输入内容时,当按下“保存”按钮时,应该保存设置(任何简单格式或任何文件都可以使用,我不受任何特定类型的约束),当按下“加载”按钮时,应显示相应的值在相应的框中。
什么是执行此操作的最简单方法。
P.S:我的设计采用tabcontrol的形式,并且框位于不同的选项卡页上,保存时应将所有选项卡页的数据保存在一个文件中。
答案 0 :(得分:1)
我建议您将其保存在已经为您准备好的“应用程序设置”中:
Properties.Settings.Default.SettingName = "Setting Value";
Properties.Settings.Default.Save();
您可以找到more info about it in here。
另一种方法是将设置保存在文本文件中并加载它们(不推荐)。
string Settings = "SomeComboBoxValue = 1\r\n" +
"SomeButtonValue = OK" //goes on like this
要保存:
File.WriteAllText("settings.txt", Settings);
要加载:
string[] lines = File.ReadAllLines("settings.txt");
foreach(string setting in lines)
{
string[] s = setting.Split('=');
switch(s[0].Trim())
{
case "SomeComboBoxValue":
ComboBox1.SelectedIndex = int.Parse(s[1].Trim()); break;
case "SomeButtonValue":
Button1.Text = s[1].Trim(); break;
//goes on like this
}
}