C#Settings.Default.Save()没有保存?

这个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. } 

单击按钮后, MessageBox将显示“Somevalue”,然后applcation重新启动,显示的MessageBox为空。 但是,通过再次单击该按钮重新启动该过程并重新启动它会显示“Somevalue” MessageBox 。 请帮忙! 非常感谢!

也许你遇到了和我一样的错误:将设置的scope设置为Application 。 这些设置不会保存

将其设置为User以解决问题。

rene是正确的 – 您需要在调用Save方法后调用Default.Reload

 Settings.Default.Save(); Settings.Default.Reload(); 

可能是一个错误 – ?

发布作为回复以提高可见度 –

经过一整天的研究和研究,我能够通过将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 (); 

小心打电话

 Settings.Default.Reload(); 

每次之后

 Settings.Default.Save(); 

Save()函数实际上将更改保存到文件中,但不会反映到正在运行的代码中。 因此,您的代码保留了以前版本文件的副本。

当您在代码中的另一个位置调用Save()时,它会覆盖您的第一个更改,从而有效地将您的第一个更改恢复为原始值。 即使在调试时也很难确定。

请看一下这个问题

特别是,从那里的答案中尝试以下代码:

 using System.Configuration; // Add a reference to System.Configuration.dll ... var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath; 

另外,看看这个概述 ,也许你遇到了一些不容易从你的问题中分辨出来的限制。

此代码项目文章可能会有所帮助。

您是否尝试在检查它已保存广告之前调用ConfigurationManager.RefreshSection ,您可以在重新加载后再次尝试

如果你需要测试你的应用程序在这种情况下如何实际工作更好地运行exe文件。 当您在调试模式下运行visual studio时,保存它的那些设置将需要一些时间。 转到调试文件夹并运行exe,您将获得预期的消息。

如果你的AssemblyInfo.cs文件在汇编版本中有* ,那么每次构建都会刷新文件,因此在将其更改为硬编号并重建所有内容之前,您将看不到持久性或可靠性,然后重新测试所有内容。

我有同样的问题。

解决问题。

转到Visual Studio中的自定义类。 打开类并检查构造函数方法是否正确。

如果你有一个构造函数方法,它应该是无参数的。 如果你有一个带参数的构造函数,请不要担心。 创建另一个没有参数的构造函数类。

对类中的所有子类重复此操作。

重建并运行。 现在你的设置应该保存。

使用Visual Studio 2013 – 我无法做任何事情让它可靠地工作,我会调用Save并且它没有保存。 保存,然后立即重新加载,它仍然不会保留后续运行的值(可能与我停止调试时无法确定根本原因相关) – 非常令人沮丧,可能存在潜在的错误但我无法certificate它。

为了避免对此感到疯狂,我决定使用注册表 – 这是保持应用程序应用程序设置的最基本方法。 推荐给大家。 这是代码:

 public static class RegistrySettings { private static RegistryKey baseRegistryKey = Registry.CurrentUser; private static string _SubKey = string.Empty; public static string SubRoot { set { _SubKey = value; } } public static string Read(string KeyName, string DefaultValue) { // Opening the registry key RegistryKey rk = baseRegistryKey; // Open a subKey as read-only RegistryKey sk1 = rk.OpenSubKey(_SubKey); // If the RegistrySubKey doesn't exist return default value if (sk1 == null) { return DefaultValue; } else { try { // If the RegistryKey exists I get its value // or null is returned. return (string)sk1.GetValue(KeyName); } catch (Exception e) { ShowErrorMessage(e, String.Format("Reading registry {0}", KeyName.ToUpper())); return null; } } } public static bool Write(string KeyName, object Value) { try { // Setting RegistryKey rk = baseRegistryKey; // I have to use CreateSubKey // (create or open it if already exits), // 'cause OpenSubKey open a subKey as read-only RegistryKey sk1 = rk.CreateSubKey(_SubKey); // Save the value sk1.SetValue(KeyName, Value); return true; } catch (Exception e) { ShowErrorMessage(e, String.Format("Writing registry {0}", KeyName.ToUpper())); return false; } } private static void ShowErrorMessage(Exception e, string Title) { if (ShowError == true) MessageBox.Show(e.Message, Title , MessageBoxButtons.OK , MessageBoxIcon.Error); } } 

用法:

 private void LoadDefaults() { RegistrySettings.SubRoot = "Software\\Company\\App"; textBoxInputFile.Text = RegistrySettings.Read("InputFileName"); } private void SaveDefaults() { RegistrySettings.SubRoot = "Software\\Company\\App"; RegistrySettings.Write("InputFileName", textBoxInputFile.Text); }