NameValueSectionHandler - 我可以使用这个部分类型写回应用程序配置文件吗? [英] NameValueSectionHandler - can i use this section type for writing back to the application config file?

查看:25
本文介绍了NameValueSectionHandler - 我可以使用这个部分类型写回应用程序配置文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回到为什么 .net 没有提供简单(我不想为 2 个值实现ConfigurationSection"ConfigurationElement"ConfigurationProperty")的方式来写回值的话题进入应用程序配置文件...(而且我不想使用用户"应用配置)

back to the topic of why didn't .net provide a simple (i don't want to implement "ConfigurationSection" "ConfigurationElement" "ConfigurationProperty" for 2 values) way to write values back into application config files... (and i don't want to use 'user' app config)

我想写入 app.config 值,我厌倦了上面的 key,value 方法 - 读取它很好,但我不能写回它(它说集合是只读的).即使提供了以下方法 -

i want to write to the app.config values, i tired the above method of key,value - for reading its fine but i can't write back into it (it says the collection is read only). even though the following method is supplied -

NameValueCollection.Set(string,string)

我在这里遗漏了什么吗?

am i missing something here ?

这是我尝试的方式:

 NameValueCollection nvc = (NameValueCollection)ConfigurationManager.GetSection("options");
 nvc.Set("SelectTimeOut", sqlTimeoutSpinBox.Value.ToString());

推荐答案

  • 没有 -NameValueSectionHandler 不会帮助用户创建"将写入 app.config 文件的 Xml.
  • 启动反射器并查看 System.Configuration.NameValueSectionHandler 上的以下方法:内部静态对象 CreateStatic(object parent, XmlNode section, string keyAttriuteName, string valueAttributeName).

    Fire up reflector and take a look at the following method on System.Configuration.NameValueSectionHandler : internal static object CreateStatic(object parent, XmlNode section, string keyAttriuteName, string valueAttributeName).

    来自 Microsoft 在线社区支持的 Linda Liu 在 EggHeadCafe 论坛的讨论中提供了一些关于 NameValueSectionHandler、IConfigurationSectionHandler 以及为什么将从 Configuration.GetSection(string) 返回 DefaultSection 实例的重要信息:System.Configuration.Configuration 的向后兼容性.

    Linda Liu from Microsoft Online Community Support gives some great information about NameValueSectionHandler, IConfigurationSectionHandler, and why a DefaultSection instance will be returned from Configuration.GetSection(string) in a discussion at the EggHeadCafe forums: Backwards compatibility of System.Configuration.Configuration.

    在技术上可以阅读和创建这些信息,但我建议您不要通过在配置 API 的这个极低级别进行交互而给自己带来不必要的痛苦.请仅将以下代码用于教育目的.我强烈建议您使用其中一种方法,例如 Richard 提到的使用声明式样式创建自定义 ConfigurationSection

    It is technically possible to read, and create this information, but I recommend you not cause yourself more pain than you must by interacting at this extremely low level of the Configuration API. Please use the below code for Educational purposes only. I highly encourage you to use one of the methods like what Richard mentioned using the declarative style for creating a custom ConfigurationSection

    app.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <section name="SingleTag" type="System.Configuration.SingleTagSectionHandler"/>
        </configSections>
        <SingleTag scheme="https" server="webmail.contoso.com" domain="CONTOSO" username="jdoe" password="iTz@s3Cr3t!"/>
    </configuration>`
    

    可以阅读此配置部分,但这对您的心理健康不利.

    You could read this configuration section, but it's not good for your mental health.

    示例 C# 代码 - 将其粘贴到您的 void Main(string[] args) 中并抽烟.

    Sample C# Code - Stick this inside your void Main(string[] args) and smoke it.

    // Read configuration
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationSection readableSection = config.GetSection("SingleTag");
    string readableSectionRawXml = readableSection.SectionInformation.GetRawXml();
    XmlDocument readableSectionRawXmlDocument = new XmlDocument();
    readableSectionRawXmlDocument.Load(new StringReader(readableSectionRawXml));
    SingleTagSectionHandler readableSectionHandler = new SingleTagSectionHandler();
    Hashtable result = (Hashtable)readableSectionHandler.Create(null, null, readableSectionRawXmlDocument.DocumentElement);
    foreach (string item in result.Keys)
    {
        Console.WriteLine("{0}\t=\t{1}", item, result[item]);
    }
    
    // Create similar configuration section
    Hashtable mySettings = new Hashtable();
    mySettings.Add("key1", "value1:" + DateTime.Now);
    mySettings.Add("key2", "value2:" + DateTime.Now);
    mySettings.Add("key3", "value3:" + DateTime.Now);
    mySettings.Add("keynull", null);
    mySettings.Add("key4", "value4:" + DateTime.Now);
    string rawData = string.Empty;
    XmlDocument writableSectionXmlDocument = new XmlDocument();
    XmlElement rootElement = writableSectionXmlDocument.CreateElement("CreateSingleTag");
    foreach (var item in mySettings.Keys)
    {
        if (mySettings[item] != null)
        {
            rootElement.SetAttribute(item.ToString(), mySettings[item].ToString());
        }
    }
    writableSectionXmlDocument.AppendChild(rootElement);
    
    if (config.Sections.Get("CreateSingleTag") == null)
    {
        ConfigurationSection writableSection = new DefaultSection();
        writableSection.SectionInformation.SetRawXml(writableSectionXmlDocument.OuterXml);
        config.Sections.Add("CreateSingleTag", writableSection);
    }
    else
    {
    config.Sections["CreateSingleTag"].SectionInformation.SetRawXml(writableSectionXmlDocument.OuterXml);
    }
    
    config.Save();
    

    为了完整起见 -您需要以下用途

    For completeness sake - you need the following usings

    using System;
    using System.Collections;
    using System.Configuration;
    using System.IO;
    using System.Xml;
    

    至少引用以下程序集

    System
    System.Configuration
    System.Xml
    

    这篇关于NameValueSectionHandler - 我可以使用这个部分类型写回应用程序配置文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆