2012-03-06 86 views
0

在web.config文件中,我有一個第三方的設置部分:閱讀第三方配置部分

<configuration> 
    <configSections> 
    <sectionGroup name="TheProduct"> 
     <section name="TheSection" type="TheCompany.TheProduct.TheSectionConfigurationHandler, TheCompany.TheProduct, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b1e9bee111e9429c" /> 
    </sectionGroup> 
    </configSections> 

    <TheProduct> 
    <TheSection somevalue="true" /> 
    </TheProduct> 
</configuration> 

我想從另一個應用程序閱讀本節的價值,但是當我嘗試找到部分,我總是得到null

這裏是我的代碼:

var config = ConfigurationManager.OpenExeConfiguration(
    @"C:\inetpub\wwwroot\TheProduct\web.config" 
    ); 
var settings = config.GetSection("TheProduct/TheSection"); // always null 

什麼是檢索配置部分的正確方法是什麼?

[編輯]如果調用應用程序,它定義了相同的部分,我可以這樣做:

var settings = ConfigurationManager.GetSection("TheProduct/TheSection"); 

,它的工作。 Mybe我沒有用正確的方式打開web.config文件?

+0

您是否在應用程序中提及了「TheCompany.TheProduct,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = b1e9bee111e9429c'? – 2012-03-06 13:08:57

+0

不......實際上,區段處理程序在第三方庫中是內部的。我想用反射來讀取值。 – 2012-03-06 13:11:33

+0

編輯:我有參考第三方lib ...我只是不能實例化類型我自己 – 2012-03-06 13:14:48

回答

0

使用配置管理器要求您的應用程序能夠實例化實際的強類型配置對象。如果您無法添加對定義程序集的引用,那麼您唯一的選擇是使用其中一個XML API(System.XmlSystem.Linq.Xml)手動讀取它,在這種情況下您根本不會使用配置管理器。

+0

我無法獲取遠程應用程序的部分,但我可以獲取當前應用程序的部分。我認爲這不是提取價值的問題,而是閱讀該部分 – 2012-03-06 13:15:19