【问题标题】:C# applicationSettings: how to update app.config?C# applicationSettings:如何更新 app.config?
【发布时间】:2012-10-28 12:34:39
【问题描述】:

我正在使用 .NET 4.0,我想使用 app.config 文件来存储相同的参数设置。我执行以下操作。我使用项目属性中的Settings 选项卡来创建我的参数。

这样在app.config文件中添加信息如下:

<MyApp.Properties.Settings>
      <setting name="param1" serializeAs="String">
        <value>True</value>
      </setting>
<MyApp.Properties.Settings>

在我的视图模型中(在我的代码中)我可以通过这种方式访问​​信息:

bool myBool = MyApp.Properties.Default.param1;

当我尝试更改配置文件中的值时,我尝试这样做:

Properties.Settings.Default.param1 = false;

但这会导致错误,即param1 是只读的。

那么如何从我的代码更新我的配置文件?

【问题讨论】:

  • 在这个解决方案中,他们使用 config.AppSettings。 AppSettings 没有被弃用?我想使用 ApplicationSettings,因为它使用类型参数。
  • 用 EDIT2 编辑了我的答案,提供了一个答案链接,该链接指向可能是您的解决方案的代码:)

标签: c# app-config


【解决方案1】:

这是我在 app.config 中为“applicationSettings”部分更新或添加条目的功能。可能有更好的方法,但这对我有用。如果有人可以提出更好的方法,请分享,我们会一直在寻找更好的方法。

    static string APPNODE = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Properties.Settings";
    static DateTime now = DateTime.Now;
    Utilities.UpdateConfig(APPNODE, "lastQueryTime", now.ToString());

    static public void UpdateConfig(string section, string key, string value)
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        ClientSettingsSection applicationSettingsSection = (ClientSettingsSection)config.SectionGroups["applicationSettings"].Sections[section];
        SettingElement element = applicationSettingsSection.Settings.Get(key);

        if (null != element)
        {
            applicationSettingsSection.Settings.Remove(element);
            element.Value.ValueXml.InnerXml = value;
            applicationSettingsSection.Settings.Add(element);
        }
        else
        {
            element = new SettingElement(key, SettingsSerializeAs.String);
            element.Value = new SettingValueElement();
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            element.Value.ValueXml = doc.CreateElement("value");

            element.Value.ValueXml.InnerXml = value;
            applicationSettingsSection.Settings.Add(element);
        }

        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("applicationSettings");            
    }

【讨论】:

    【解决方案2】:

    好吧,我读了 Hari Gillala 的链接,其中一位用户建议直接编辑 app.config 文件,即 xml 文件。

    所以在项目属性-->设置中我创建了我需要的参数。然后,要在代码中加载参数,我执行以下操作:

    _myViewModelProperty = MyApp.Properties.Settings.Default.MyParam1;
    

    这样,我可以很容易地读取配置参数的信息。已输入,因此在设计时我可以查看分配是否正确。

    为了更新 de config 文件,我使用 .NET 的 xml 库编辑 app.config 文件。

        System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
        xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        System.Xml.XmlNode node;
        node = xml.SelectSingleNode("configuration/applicationSettings/MyApp.Properties.Settings/setting[@name='myparam1']");
        node.ChildNodes[0].InnerText = myNewValue;
        xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    

    这样,我创建了一个xml文档(xml变量)并加载了app.config文件的信息。然后,我搜索要更新的节点,更新其信息(InnerText 属性),最后保存更改。

    这样,我可以更新app.config。这是我想要的,因为在便携式应用程序中,只有一个用户会使用它,并且我希望在运行该应用程序的任何计算机上应用该配置。

    【讨论】:

      【解决方案3】:

      您应该使用Properties.Settings 来执行此操作。 您可以查看此documentation 了解更多信息。

      //modify
      Properties.Settings.Default.param1 = false;
      //save the setting
      Properties.Settings.Default.Save();
      

      请注意,如果您的设置具有用户范围,它们必须是可写的,那么这些属性将保存在其他地方,而不是您的本地配置文件中。详情请见here

      在评论和进一步搜索中讨论后编辑:

      我的建议是切换到 AppSettings。 那是因为,经过一些搜索,我发现 appsettings 更改了 Application Data 文件夹中的 .config 文件(在我的机器上运行一些测试确认了这一点)。

      查看this question的回复中的评论。

      我不确定是否有一些解决方法,但如果您希望您的应用程序有一个可移植的 app.config 文件,我认为唯一的方法是切换到我的 AppSettings确定可以保存在程序文件夹中找到的 app.config 中的更改。

      编辑 2:可能的解决方案

      我找到了一种可能的解决方案,可以让您的应用可移植! 您可以更改 Settings 使用的 Provider 以保存应用程序的设置,从而创建自定义 Provider。

      this question 的答案提供了一个代码链接,以使应用程序设置可移植。我想你试试看

      【讨论】:

      • 好吧,我真的用这种方式,Properties.Settings.Default,但是我写错了我的代码。这就是我得到错误的方式。
      • 我认为您的设置范围是应用程序,您必须将其更改为用户才能在运行时对其进行修改
      • 在 app.config 中,您可以将设置从 ... 部分移动到 部分,或者使用 GUI 有一个设置配置中的范围组合框
      • 我尝试使用这种方式,但文件 myApp.exe.config 没有更新。但是我重新启动我的应用程序,更改被使用,所以我认为配置保存在其他地方,但我的程序文件夹中没有其他文件。
      • 我试过了,我得到了同样的结果。我不完全知道它保存在哪里...你为什么不使用 ConfigurationManager 移动到 AppSettings?
      【解决方案4】:

      将您的设置标记为用户设置。

      详文:http://www.codeproject.com/Articles/25829/User-Settings-Applied

      【讨论】:

        猜你喜欢
        • 2019-01-06
        • 2011-09-14
        • 2011-01-07
        • 1970-01-01
        • 2012-12-24
        • 2011-08-05
        • 2010-10-02
        • 1970-01-01
        • 2012-03-18
        相关资源
        最近更新 更多