C#applicationSettings:如何更新app.config?

我使用的是.NET 4.0,我想使用app.config文件来存储相同的参数设置。 我做了以下几点。 我使用项目属性中的Settings选项卡来创建参数。

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

   True   

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

 bool myBool = MyApp.Properties.Default.param1; 

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

 Properties.Settings.Default.param1 = false; 

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

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

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

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

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

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

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

通过这种方式,我可以轻松阅读config参数的信息。 是键入的,所以在设计时我可以看到asign是否正确。

要更新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。 这是我想要的,因为在便携式应用程序中,只有一个用户会使用它,我希望配置应用于我运行应用程序的任何计算机。

您应该使用Properties.Settings来执行此操作。 您可以查看此文档以获取更多信息。

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

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

我建议实现所需的结果是切换到AppSettings 。 这是因为,在一些搜索之后,我发现appsettings更改Application Data文件夹中的.config文件(在我的机器上运行一些测试确认)。

看看这个问题的答案中的评论。

我不确定是否有一些工作,但如果你想让你的应用程序有一个便携式app.config文件,我认为唯一的方法是切换到AppSettings ,我相信我可以保存在app.config找到的更改在程序文件夹中。

编辑2:可能的解决方案

我发现了一个可能的解决方案,让您的应用程序可移植 您可以更改“设置”使用的“提供程序”以保存创建自定义提供程序的应用程序设置。

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

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

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