- ん - NameValueSectionHandlerは、app.configファイルに書き込まれるXMLを「作成」でユーザを支援しません。
火災アップリフレクターをしてSystem.Configuration.NameValueSectionHandlerに以下の方法を見てみ取る:内部の静的オブジェクトCreateStatic(オブジェクトの親、XmlNodeのセクション、文字列keyAttriuteName、文字列valueAttributeNameを)。
マイクロソフトオンラインコミュニティサポートから林達劉はNameValueSectionHandler、IConfigurationSectionHandlerに関するいくつかの素晴らしい情報を与え、そしてなぜDefaultSectionインスタンスがEggHeadCafeフォーラムでの議論にConfiguration.GetSection(文字列)から返されます。Backwards compatibility of System.Configuration.Configuration。
この情報を読んで作成することは技術的に可能ですが、この極端に低いレベルのConfiguration APIでやり取りする必要があるよりも、苦痛を伴うことはありません。 教育用の目的のために、以下のコードを使用してください。 のようなメソッドを使用して、カスタムConfigurationSectionを作成するための宣言的スタイルを使用するような方法を使用することを強くお勧めします。
appコンフィグ
<?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="[email protected]!"/>
</configuration>`
あなたはこの構成セクションを読むことができるが、それはあなたの精神的健康のために良いではありません。
サンプルC#コード - これをあなたのvoid Main(string [] args)の中に貼り付け、煙らせます。完全性期すため
// 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();
- 次usings
using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Xml;
と「正しい方法で設定をロード」について、少なくとも次のアセンブリ
System
System.Configuration
System.Xml
出典
2010-02-03 19:19:46
JJS
への参照が必要です。 ... 現在実行中: NameValueCollection nvc =(NameValueCollection)ConfigurationManager.GetSecti on( "options"); nvc.Set( "SelectTimeOut"、sqlTimeoutSpinBox.Value.ToString()); は間違っていますか?どのように私は 'nvc'をインスタンス化する必要がありますか? – thedrs
これに後で戻ってくる必要があります...これを行うコードを調べる必要がありますので、私はあなたに正しい場所を提供します。その間、完全な構成を返すConfigurationManagerのメソッドを見てください。 – Richard
ありがとうございますが、主な問題は*アプリケーション* app.config(exeの近くに座っているもの)に書き込むことができませんでした。 どういうわけかMSは、それをするのが最も難しいと決めました。 (ConfigSection/Element/...クラスを実装する必要があります)。 * user * appの設定への書き込みは非常に簡単です。あなたは.net対アプリケーションに組み込まれているSettingsクラスを使うことができます。 – thedrs