2009-07-10 37 views
1

有沒有辦法在app.config文件中寫入有關端點的信息? 其實我想讀取一個app.config文件中的<service>標記,並且希望將它寫入其他app.config文件的<Services>標記中。如何在運行時WCF的app.config中編寫端點信息?

請別人告訴我該怎麼辦?

其實我有所謂的「WCFService.exe.config」,我想在我的程序來讀取配置文件,所以我正在寫的是:

string path = Path.Combine(Application.StartupPath, "WCFService.exe.config"); 
Configuration config = ConfigurationManager.OpenExeConfiguration(path) 

ServicesSection serviceSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection; 

ServiceElementCollection sereleColl = serviceSection.Services; 

,但我什麼也沒有在sereleColl。

回答

2

看看在ConfigurationManager class

我不記得如何做到這一點,雖然。

編輯:

要訪問它,你必須添加到System.Configuration參考。

編輯2:

改變的AppSettings可以做這樣的:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); 
AppSettingsSection appSettings = config.AppSettings; 
KeyValueConfigurationElement setting = appSettings.Settings["MyAppSettingsKey"]; 
setting.Value = "newValue"; 
config.Save(ConfigurationSaveMode.Modified); 
ConfigurationManager.RefreshSection("appSettings"); 

您可以通過鍵入訪問WCF設置:

ConfigurationSectionGroup sct = config.SectionGroups["system.serviceModel"]; 

希望這有助於。

評論:

你的代碼工作在這裏很好。不過,我改變

string path = Path.Combine(Application.StartupPath, "WCFService.exe.config"); 

string path = Application.ExecutablePath; 

這將使用當前運行的應用程序的配置文件。我不知道你的路徑爲什麼不起作用。要麼是,要麼你的配置文件中必須有錯誤?

+0

實際上我不想讀取當前可執行文件的app.config,但我想讀取另一個「WCFService.exe.config」的app.config文件。 – 2009-07-10 09:50:46

+0

我仍然可以使用你的方法加載其他配置。 – Ezombort 2009-07-10 10:02:18

+0

我唯一的想法是,你的配置文件中一定有錯誤,但我想你已經檢查過了.-) – Ezombort 2009-07-10 10:05:15

3

您的調用「OpenExeConfiguration」 - 這將打開當前正在執行的應用程序的配置。

您需要到.NET 2.0配置系統上讀了起來:還有的.NET配置系統上的卓越的三部分組成的系列在CodeProject:

有一系列對你真的好文章,澄清其的。 NET 2。在CodeProject 0配置系統:

  1. Unraveling the mysteries of .NET 2.0 configuration

  2. Decoding the mysteries of .NET 2.0 configuration

  3. Cracking the mysteries of .NET 2.0 configuration

強烈推薦! Jon Rista在.NET 2.0中解釋配置系統做得很好。

相關問題