2012-03-06 88 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