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

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

问题描述

回为什么没有.NET提供话题的简单的(我不希望实施配置节的ConfigurationElement的ConfigurationProperty2的值)的方式来写值回到应用程序配置文件... (我不想用'用户'的应用程序配置)

我要写信给在app.config值,关键我累了上述方法,价值 - 读它的罚款,但我不能回写进去(它说的集合是只读)。 尽管下面的方法被提供 -

  NameValueCollection.Set(字符串,字符串)
 

我失去了一些东西呢?

这是这样的,我试图做到这一点:

 的NameValueCollection雷士=(NameValueCollection中)ConfigurationManager.GetSection(「购股权」);
 nvc.Set(SelectTimeOut,sqlTimeoutSpinBox.Value.ToString());
 

解决方案
  • 否 - NameValueSectionHandler不能帮助用户创造将被写入到app.config文件的XML。

火了反光镜和看一看上System.Configuration.NameValueSectionHandler下面的方法:内部静态对象CreateStatic(对象父,XmlNode的部分,串keyAttriuteName,串valueAttributeName)

林达刘从微软在线社区支持提供有关NameValueSectionHandler,IConfigurationSectionHandler,为什么一个DefaultSection实例将从Configuration.GetSection(串)的讨论在EggHeadCafe论坛返回一些伟大的信息:的的/www.eggheadcafe.com/software/aspnet/30832856/backwards-compatibility-o.aspx相对=nofollow>向后兼容性。

这在技术上是可以读取,创建此信息,但我建议你不要自己造成更多的痛苦比你必须通过在这个极低的配置API的级别进行交互。请使用以下$ C $下的教育仅目的。我强烈建议你使用像什么理查德使用声明样式创建自定义配置节

的app.config

 < XML版本=1.0编码=UTF-8&GT?;
<结构>
    < configSections>
        <节名称=SingleTagTYPE =System.Configuration.SingleTagSectionHandler/>
    < / configSections>
    < SingleTag计划=htt​​ps开头的服务器=webmail.contoso.com域=CONTOSO用户名=JDOE密码=!伊茨@ s3Cr3t/>
< /结构>`
 

您的可以的读取配置部分,但它不利于你的心理健康。

样品C#code - 棒这是你的无效的主要(字串[] args)和吸烟这里面

  //读取配置
配置配置= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
的ConfigurationSection readableSection = config.GetSection(SingleTag);
字符串readableSectionRawXml = readableSection.SectionInformation.GetRawXml();
XmlDocument的readableSectionRawXmlDocument =新的XmlDocument();
readableSectionRawXmlDocument.Load(新StringReader(readableSectionRawXml));
SingleTagSectionHandler readableSectionHandler =新SingleTagSectionHandler();
Hashtable的结果=(哈希表)readableSectionHandler.Create(NULL,NULL,readableSectionRawXmlDocument.DocumentElement);
的foreach(在result.Keys字符串项)
{
    Console.WriteLine({0} \ T = \ t {1}项,造成[项目]);
}

//创建类似的配置节
Hashtable的mySettings =新的Hashtable();
mySettings.Add(键1,值1:+ DateTime.Now);
mySettings.Add(KEY2,值2:+ DateTime.Now);
mySettings.Add(KEY3,值3:+ DateTime.Now);
mySettings.Add(keynull,NULL);
mySettings.Add(KEY4,值4:+ DateTime.Now);
字符串RAWDATA =的String.Empty;
XmlDocument的writableSectionXmlDocument =新的XmlDocument();
的XmlElement rootElement的= writableSectionXmlDocument.CreateElement(CreateSingleTag);
的foreach(在mySettings.Keys VAR项)
{
    如果(mySettings [项目]!= NULL)
    {
        rootElement.SetAttribute(item.ToString(),mySettings [项目]的ToString());
    }
}
writableSectionXmlDocument.AppendChild(rootElement的);

如果(config.Sections.Get(CreateSingleTag)== NULL)
{
    配置节writableSection =新DefaultSection();
    writableSection.SectionInformation.SetRawXml(writableSectionXmlDocument.OuterXml);
    config.Sections.Add(CreateSingleTag,writableSection);
}
其他
{
config.Sections["CreateSingleTag"].SectionInformation.SetRawXml(writableSectionXmlDocument.OuterXml);
}

config.Save();
 

有关完整的缘故 - 您需要以下usings

 使用系统;
System.Collections中使用;
使用System.Configuration;
使用System.IO;
使用的System.Xml;
 

和的引用至少以下组件

 系统
系统配置
的System.Xml
 

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)

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 ?

this is the way i am trying to do it:

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

解决方案

  • No - NameValueSectionHandler does not assist the user in 'creating' the Xml that will be written to the app.config file.

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).

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.

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.

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;

and a reference to at least the following assemblies

System
System.Configuration
System.Xml

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

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