这个bug很不寻常.基本上我的代码将更改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.
}
Run Code Online (Sandbox Code Playgroud)
该MessageBox会显示"someValue中"单击时按钮,然后在重新启动的applcation和MessageBox那表明是空的.但是,再次单击该按钮重复该过程并重新启动它会显示"Somevalue" MessageBox.请帮忙!非常感谢!
Dav*_*her 38
也许你和我一样犯了同样的错误:将设置设置scope为Application.这些设置不会保存.
将其设置User为解决问题.
rene 是正确的 - 您需要Default.Reload在调用Save方法后调用:
Settings.Default.Save();
Settings.Default.Reload();
Run Code Online (Sandbox Code Playgroud)
可能是一个错误 - ?
发布作为回复以提高知名度 -
小智 5
经过一整天的研究和研究,我可以通过将Configuration投放给用户来解决此问题:
Using System.Configuration;
Properties.Settings.Default.strinconn = txt_stringconn.Text;
Properties.Settings.Default.Save ();
Properties.Settings.Default.Upgrade ();
MessageBox.Show ("Saved Settings");
Application.Restart ();
Run Code Online (Sandbox Code Playgroud)
请看看这个问题
特别是,请尝试以下答案中的代码:
using System.Configuration; // Add a reference to System.Configuration.dll
...
var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
Run Code Online (Sandbox Code Playgroud)
另外,请查看此概述,也许您遇到了一些限制,这些限制很难从您的问题中看出。
这篇代码项目文章可能会有所帮助。