【发布时间】:2013-05-28 16:47:04
【问题描述】:
这个错误很不寻常。基本上我的代码将更改Settings.Default.Example 然后保存并重新启动程序。然后当它加载时,它会显示一个消息框。然而奇怪的是,当表单加载时它显示一个空值。
这是我的代码:
Main.cs
private void Button1_Click(object sender, EventArgs e)
{
Settings.Default.Example = "Somevalue"; //Sets a value to the settings
Settings.Default.Save(); // Save it
MessageBox.Show(Settings.Default.Example); //Confirming it has been saved
Application.Restart();
}
private void Main_Load(object sender, EventArgs e)
{
MessageBox.Show(Settings.Default.Example); // Here is the weird part, it shows empty.
}
单击按钮时MessageBox 将显示“Somevalue”,然后应用程序重新启动并且显示的MessageBox 为空。但是,通过再次单击按钮并重新启动它来重复该过程确实会显示“Somevalue”MessageBox。请帮忙!非常感谢!
【问题讨论】:
-
保存后有User.config文件吗?
-
对不起,我没有完全理解它
-
尝试将您的消息框移动到显示的事件,您的设置可能尚未加载
-
添加一个 Settings.Default.Reload();保存后。