【问题标题】:C# Settings.Default.Save() not saving? [duplicate]C# Settings.Default.Save() 不保存? [复制]
【发布时间】: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();保存后。

标签: c# settings


【解决方案1】:

也许你犯了和我一样的错误:将设置的scope 设置为Application。这类设置没有保存

设置为User即可解决问题。

【讨论】:

  • 知道为什么应用程序范围不保存设置吗?感觉他们应该……
  • @TinyRacoon 应用程序范围设置旨在类似于“对应用程序至关重要的事物”,例如连接字符串。因此,提供管理员通过在文本编辑器中更改 applicationname.exe.config 来编辑它们足以实现普通用户不需要关心的更改(出于安全原因,他们不应该关心)。用户范围的设置可用于改变每个用户的体验,相关的 xml 文件存储在文件系统的一个区域中,无需提升权限即可写入
【解决方案2】:

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

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

可能是一个错误 - ?

作为回复发布以提高知名度 -

【讨论】:

    【解决方案3】:

    经过一整天的研究和研究,我能够通过将配置交给用户来解决这个问题:

    Using System.Configuration; 
    
    Properties.Settings.Default.strinconn = txt_stringconn.Text;
    Properties.Settings.Default.Save ();
    Properties.Settings.Default.Upgrade ();
    MessageBox.Show ("Saved Settings");
    Application.Restart ();
    

    【讨论】:

      【解决方案4】:

      如果您的AssemblyInfo.cs 文件在Assembly Version 中有*,那么它会在每次构建时刷新文件,因此您不会看到持久性或可靠性,直到您将其更改为硬数字并重新构建所有内容,然后重新测试所有内容。

      【讨论】:

      • 或添加类似字符串 appVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); if (Settings.Default.ApplicationVersion != appVersion) { Settings.Default.Upgrade(); Settings.Default.ApplicationVersion = appVersion; Settings.Default.Save(); }
      • 我使用自动版本扩展并为此项目禁用它修复了下次启动后无法重新加载设置的问题。我想知道,有没有办法通过不同的版本保持设置持久化?
      【解决方案5】:

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

      为了避免对此感到抓狂,我决定使用注册表 - 为应用保留应用设置的最基本方法。 推荐给大家。代码如下:

      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);
      }
      

      【讨论】:

      【解决方案6】:

      小心打电话

      Settings.Default.Reload();
      

      每个

      之后
      Settings.Default.Save();
      

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

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

      【讨论】:

        【解决方案7】:

        请看this Question

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

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

        另外,请查看this overview,也许您遇到了一些从您的问题中不容易看出的限制。

        This codeproject article 可能会有所帮助。

        【讨论】:

        • 感谢您的建议。正如我在问题中提到的那样,我尝试重复单击按钮的过程,并在第二次重新启动时,消息框可以显示值......这意味着它已经被保存在第一位。
        【解决方案8】:

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

        【讨论】:

          【解决方案9】:

          如果您需要测试您的应用程序在这种情况下的实际工作方式,最好运行 exe 文件。当您在调试模式下在 Visual Studio 上运行时,保存这些设置需要一些时间。转到调试文件夹并运行 exe,您将按预期收到消息。

          【讨论】:

            【解决方案10】:

            我遇到了同样的问题。

            解决问题。

            转到 Visual Studio 中的自定义类。 打开类,检查是否有构造方法。

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

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

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

            【讨论】:

              猜你喜欢
              • 2015-08-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-11-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多