2009-03-09 19 views

答えて

2

を、私は常にXmlDocumentとXPathを使用し、2で仕事をしてくれました追加のアセンブリ参照を必要とせずに、コード行とインポートされた1つの名前空間を使用できます。

WebConfigurationManager.OpenWebConfiguration("/", "My Website", null) 

上記には、仮想パスが必要です。これは、WinFormsアプリケーションに何を意味するのでしょうか?私は、WebForms 'をWinFormsアプリケーション'で開く必要があると述べました。

私はXPathを改善しました。 :-)

0

ハムは、あなたがしようとしたんでした:それと

Using System.Web.Configuration; 

または

Using System.Configuration; 

をあなたはweb.configファイルにアクセスできるようになりますConfigurationManagerオブジェクトへのアクセス権を持っている必要があります。

1

WebConfigurationManager.OpenWebConfiguration methodは、System.Web.Configuration名前空間で使用できます。

例:

System.Configuration.Configuration config = 
     WebConfigurationManager.OpenWebConfiguration("/", "My Website", null) as System.Configuration.Configuration; 

KeyValueConfigurationCollection appSettings = config.AppSettings.Settings; 

これは「私のウェブサイトのウェブサイトののAppSettingsブロックを取得します。 configファイルへの仮想パスである最初のパラメータを変更するだけです。あなたはあなたがたSystem.Diagnosticsのようなパブリックインタフェースを持たない構成セクションにアクセスしたい場合は

using System.Web.Configuration 
WebConfigurationManager webCfg = WebConfigurationManager.OpenWebconfiguration("/WebApp"); 

// Edit/Query the configuration 
webConfig.AppSettings.Settings.Add("NewSetting","SomeValue"); 
webConfig.AppSettings.SectionInformation.ForceSave = true; 

webConfig.Save(); 

2

あなたは、その後のようなものを行くと行くのSystem.Webへの参照を追加する必要があります一般的な構成クラスを使用できます。 System.Configurationへの参照を追加し、何かしてみてください:失敗の度合いを変化させて、上記の、非常に迅速にいえ、すべての提案を試みた後

ConfiguationSection sysDiagnostics = webConfig.GetSection("system.diagnostics"); 
ConfigurationElementCollection sources = sysDiagnostics.ElementInformation.Properties["sources"].Value as ConfigurationElementCollection; 
foreach (ConfigurationElement source in sources) 
{ 
    ConfigurationElementCollection listeners = source.ElementInformation.Properties["listeners"].Value as ConfigurationElementCollection; 
    foreach (ConfigurationElement listener in listeners) 
    { 
     Console.WriteLine(listener.ElementInformation.Properties["name"].Value.ToString()); 
    } 
} 
関連する問題