2009-07-19 2 views
1

なぜ戻っていないのですか。(私は2つの値の "ConfigurationSection" "ConfigurationElement" "ConfigurationProperty"を実装したくありません)アプリケーション設定ファイルに値を書き込む方法はありません。 。 (私は 'user' app configを使用したくない)NameValueSectionHandler - このセクションタイプをアプリケーションの設定ファイルに書き戻すために使用できますか?

私はapp.configの値に書きたいと思っています。私は上記のkey、valueのメソッドを疲れました。それに書き戻します(コレクションは読み取り専用です)。 次のような方法が供給されているにもかかわらず -

NameValueCollection.Set(string,string) 

私はここで何かが足りないのですか?

これは私がそれをやろうとしています方法です:

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

答えて

1

あなたは正しい方法で設定ファイルをロードする必要があります。 ConfigurationManagerの静的プロパティを使用するのではなく、ロードするメソッドを使用します。

また、グローバル、アプリケーション、ユーザーローミングとユーザーローカル設定の違いを管理していることを確認する必要があります。通常、最後の2つだけが書き込み可能でなければなりません。

使用設定ファイルへの書き込みの変更のためのいくつかのテストコード:

TestConfigDateは、カスタム構成タイプである
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); 
TestConfigData data = (TestConfigData)config.GetSection("testConfigData"); 
++data.Data; 
config.Save(ConfigurationSaveMode.Minimal); 

:上allowExeDefinition属性に注目し、

using System; 
using System.Configuration; 
using System.Text; 

namespace CustomConfiguration { 
    public class TestConfigData : ConfigurationSection { 

    [ConfigurationProperty("Name", IsRequired=true)] 
    public string Name { 
     get { 
     return (string)this["Name"]; 
     } 
     set { 
     this["Name"] = value; 
     } 
    } 

    [ConfigurationProperty("Data", IsRequired=false), 
    IntegerValidator(MinValue=0)] 
    public int Data { 
     get { 
     return (int)this["Data"]; 
     } 
     set { 
     this["Data"] = value; 
     } 
    } 
    } 
} 

そして、含まれている設定ファイルsection要素を使用して、ユーザー設定ファイルを定義して、このファイルを上書きします。app.exe.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="testConfigData" 
      type="CustomConfiguration.TestConfigData, CustomConfiguration" 
      allowExeDefinition="MachineToLocalUser"/> 
    </configSections> 

    <testConfigData Name="Fubar" Data="0"/> 
</configuration> 
+0

への参照が必要です。 ... 現在実行中: NameValueCollection nvc =(NameValueCollection)ConfigurationManager.GetSecti on( "options"); nvc.Set( "SelectTimeOut"、sqlTimeoutSpinBox.Value.ToString()); は間違っていますか?どのように私は 'nvc'をインスタンス化する必要がありますか? – thedrs

+0

これに後で戻ってくる必要があります...これを行うコードを調べる必要がありますので、私はあなたに正しい場所を提供します。その間、完全な構成を返すConfigurationManagerのメソッドを見てください。 – Richard

+0

ありがとうございますが、主な問題は*アプリケーション* app.config(exeの近くに座っているもの)に書き込むことができませんでした。 どういうわけかMSは、それをするのが最も難しいと決めました。 (ConfigSection/Element/...クラスを実装する必要があります)。 * user * appの設定への書き込みは非常に簡単です。あなたは.net対アプリケーションに組み込まれているSettingsクラスを使うことができます。 – thedrs

2
  • ん - 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 
+0

ありがとうございました。構成ハンドラーを使用する従来のコードをテストしようとしています。 – Bronumski

+0

@ Bronumskiに感謝します。私はあなたを助けてくれてうれしいです。あなたがあなたが答えた古い投稿を見たときに私がするほど笑うことを願っています。私は1年前に何を考えていたのか分かりませんでした。 – JJS

+0

2年前からヒントのためにとても感謝しています...これは今日私を本当に助けました! – JJS

関連する問題