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

Álv*_*cía 5 c# app-config

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

这样就可以app.config像这样在文件中添加信息:

<MyApp.Properties.Settings>
      <setting name="param1" serializeAs="String">
        <value>True</value>
      </setting>
<MyApp.Properties.Settings>
Run Code Online (Sandbox Code Playgroud)

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

bool myBool = MyApp.Properties.Default.param1;
Run Code Online (Sandbox Code Playgroud)

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

Properties.Settings.Default.param1 = false;
Run Code Online (Sandbox Code Playgroud)

但这会导致错误,即param1只读.

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

Man*_*nny 10

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

    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");            
    }
Run Code Online (Sandbox Code Playgroud)


Álv*_*cía 5

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

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

_myViewModelProperty = MyApp.Properties.Settings.Default.MyParam1;
Run Code Online (Sandbox Code Playgroud)

这样,我可以轻松读取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);
Run Code Online (Sandbox Code Playgroud)

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

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