16

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

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

无论如何,我们公司的机器仅由一位用户使用,因此通常不需要用户特定的设置。

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

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

4

3 回答 3

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

希望有帮助!

于 2010-09-09T17:22:33.127 回答
7

这是允许您更改条目的方法<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更新后刷新该部分。

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

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

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

        catch (Exception) { throw; }

将异常抛出给调用者。或者,为了调试,您可以添加:

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

如果您选择了“调试”配置,它将在 Visual Studio的输出窗口中显示异常,但将继续执行代码。


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

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

  • 如果您不确定是否应该使用AppSettingsor applicationSettings,请在决定之前阅读此内容。

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

  • 如果您使用的是.NET Core框架,请查看此链接:.NET Core 中的 AppSettings

于 2012-08-07T07:27:57.627 回答
3

WPF 应用程序能够像 WinForms 应用程序一样通过

ConfigurationManager.OpenExeConfiguration()

方法。诀窍是在 App.config 文件的 AppSettings 标记中包含要访问的值(在 WPF 应用程序中也可用)。

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

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

不久前我写了一个完整的“如何做”,在这里解释了这一切。

于 2010-09-09T17:30:35.570 回答