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

Tim*_*ord 14 .net wpf app-config appsettings visual-studio

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

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

我们公司的机器仅供一个用户使用,因此通常不需要用户特定的设置.

否则我真的很喜欢使用应用程序设置,并希望尽可能继续使用它们.如果设置可以与EXE位于同一个文件夹中(比如曾经做过的好的'ini文件),那将是理想的选择.

注意:这是一个WPF应用程序,而不是ASP.net Web应用程序,所以没有web.config.

Tas*_*ask 9

好吧,我还没有想在运行时更改应用程序设置(这就是我使用的用户设置),但我能够做的是在安装时编写应用程序设置.我认为类似的方法可能在运行时工作.您可以尝试一下,因为似乎没有任何其他提出的解决方案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();
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!


Mat*_*att 6

这是允许您更改以下内容中的条目的方法<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
Run Code Online (Sandbox Code Playgroud)

请注意,我发现有必要在更新appSettings后刷新该部分.

该函数在添加密钥之前删除密钥以避免双重输入.如果以前不存在密钥,这也适用.如果有任何错误,则返回false,成功为true.读取设置的方法很简单,只是为了完整性而列出:

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

请注意,我通过try ... finally块来包围它以抑制错误.如果发生任何错误,则GetSetting只返回null,而SetSetting返回false.这使处理更容易,但是如果您需要例外,您仍然可以添加

        catch (Exception) { throw; }
Run Code Online (Sandbox Code Playgroud)

将异常抛给调用者.或者,为了进行调试,您可以添加:

        #if DEBUG
        catch (Exception ex) { 
                System.Diagnostics.Debug.WriteLine(ex.ToString()); 
        }
        #endif
Run Code Online (Sandbox Code Playgroud)

如果您已选择"调试"配置,将在Visual Studio的" 输出"窗口中显示异常,但将继续使用代码.


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

  • 的applicationSettings部分是不同的,因为它区分了"用户"和"应用程序"范围之间,它支持不同的数据类型,而不只是字符串.如果您想知道如何处理applicationSettings,可以在这里找到它(在stackoverflow上):
    如何访问applicationSettings

  • 如果您不确定是否应该使用AppSettingsapplicationSettings,在决定之前阅读此内容.

  • 如果您遇到警告'ConfigurationSettings.AppSettings' is obsolete,那么此提示可以帮助您.