代码之家  ›  专栏  ›  技术社区  ›  Tim Santeford

如何在运行时读取/写入app.config设置而不使用用户设置?

  •  14
  • Tim Santeford  · 技术社区  · 16 年前

    我正在寻找一种存储应用程序或机器级设置的方法,可以在运行时使用 Application Settings . 用户设置允许读/写,但应用程序设置不允许。我一直在使用用户设置在运行时保存这样的设置,但事实证明这是不切实际的,原因如下:

    • 机器的所有用户都需要共享设置。
    • 在支持呼叫中(特别是在危机情况下),很难向用户/员工解释在哪里手动查找和修改这些设置(AppData是一个隐藏文件夹等)。
    • 新版本的应用程序需要使用以前的设置(用户设置似乎会被新版本吹走)。
    • 我们的员工通常会将应用程序复制到一个新文件夹中,该文件夹也会重置用户设置。

    我们公司的机器只能由一个用户使用,因此通常不需要用户特定的设置。

    否则,我真的很喜欢使用应用程序设置,如果可能,我想继续使用它们。如果设置可以位于 与exe相同的文件夹 (就像以前的好文件一样)。

    注: 这是一个WPF应用程序,而不是ASP.NET Web应用程序,因此没有web.config。

    3 回复  |  直到 9 年前
        1
  •  9
  •   Task    16 年前

    嗯,我还不想在运行时更改应用程序设置(这是我使用用户设置的目的),但我能做的是在安装时编写应用程序设置。我认为类似的方法可能在运行时工作。你可以试试,因为似乎没有其他的ATM解决方案。

        exePath = Path.Combine( exePath, "MyApp.exe" );
        Configuration config = ConfigurationManager.OpenExeConfiguration( exePath );
        var setting = config.AppSettings.Settings[SettingKey];
        if (setting != null)
        {
            setting.Value = newValue;
        }
        else
        {
            config.AppSettings.Settings.Add( SettingKey, newValue);
        }
    
        config.Save();
    

    希望有帮助!

        2
  •  6
  •   Matt    9 年前

    这是允许您更改 <AppSettings> :

        internal static bool SetSetting(string Key, string Value)
        {
            bool result = false;
            try
            {
                System.Configuration.Configuration config =
                  ConfigurationManager.OpenExeConfiguration(
                                       ConfigurationUserLevel.None);
    
                config.AppSettings.Settings.Remove(Key); 
                var kvElem= new KeyValueConfigurationElement(Key, Value);
                config.AppSettings.Settings.Add(kvElem);
    
                // Save the configuration file.
                config.Save(ConfigurationSaveMode.Modified);
    
                // Force a reload of a changed section.
                ConfigurationManager.RefreshSection("appSettings");                
    
                result = true;
            }
            finally
            { }
            return result;
        } // function
    

    注释 我发现有必要刷新该节 appSettings 更新后。

    函数在添加键之前删除它,以避免出现重复项。如果密钥以前不存在,也可以使用此方法。如果有任何错误,它会返回 假 关于成功 真 . 读取设置的方法很简单,只是为了完整性而列出:

        internal static string GetSetting(string Key)
        {
            string result = null;
            try
            {
                result = ConfigurationManager.AppSettings[Key];
            }
            finally
            { }
            return result;
        } // function
    

    注释 我用一个 尝试。。。最后 阻止以抑制错误。如果出现任何错误,那么getsetting只返回空值,而setsetting返回假值。这使得处理更容易,但是如果您需要例外,您仍然可以添加

            catch (Exception) { throw; }
    

    向调用方引发异常。或者,为了调试,您可以添加:

            #if DEBUG
            catch (Exception ex) { 
                    System.Diagnostics.Debug.WriteLine(ex.ToString()); 
            }
            #endif
    

    它将在 产量 如果选择了“调试”配置,则将继续执行代码。


    注(交叉引用类似主题):

    • 这个 应用程序设置 部分是不同的,因为它区分“用户”和“应用程序”范围,并且它支持不同的数据类型,而不仅仅是字符串。如果你想知道如何处理 应用程序设置 ,您可以在此处找到它(stackoverflow上):
      How to access applicationSettings

    • 如果您不确定是否应该使用 AppSettings 或 applicationSettings 然后 read this 在你决定之前。

    • 如果你遇到警告 'ConfigurationSettings.AppSettings' is obsolete 然后 this hint can help you.

        3
  •  3
  •   Dillie-O    16 年前

    WPF应用程序能够访问app.config文件,就像通过

    ConfigurationManager.OpenExeConfiguration()
    

    方法。诀窍是在app.config文件的appsettings标记中拥有您想要访问的值(也可以在wpf应用程序中使用)。

    所有这些的诀窍是确保在修改完属性后调用以下方法:

    MyConfig.Save(ConfigurationSaveMode.Modified)
    ConfigurationManager.RefreshSection("appSettings")
    

    我写了一个完整的“怎么做”来解释这一切。 here .

    推荐文章