如何获得类型NameValueSectionHandler的配置节的值 [英] How to get the values of a ConfigurationSection of type NameValueSectionHandler
问题描述
我用C#的工作,框架3.5(VS 2008)。
我使用了 ConfigurationManager中
来一个配置(不是默认app.config文件)加载到配置对象。
使用配置类,我能得到一个配置节
,但我无法找到一个方法来得到部分的值。
在配置中,配置节
的类型为 System.Configuration.NameValueSectionHandler
。
对于什么是价值,当我使用的方法 GetSection
,当它是在 ConfigurationManager中
(仅我的默认app.config文件),我收到了一个对象类型,我可以投进去对键 - 值的集合,我刚刚收到就像一本字典的值。当我收到我不能做这样的投配置节
从配置类然而类。
编辑:
配置文件的例子:
<结构>
< configSections>
<节名称=MyParams
键入=System.Configuration.NameValueSectionHandler/>
< / configSections> < MyParams>
<添加键=FirstParamVALUE =一个/>
<添加键=SecondParamVALUE =二/>
< / MyParams>
< /结构>
的办法,我能够使用它时,它是对的app.config示例(以下简称GetSection的方法是默认的app.config只):
的NameValueCollection myParamsCollection =
(NameValueCollection中)ConfigurationManager.GetSection(MyParams);Console.WriteLine(myParamsCollection [FirstParam]);
Console.WriteLine(myParamsCollection [SecondParam]);
<一个href=\"https://web.archive.org/web/20131006233937/http://bloggingabout.net/blogs/jschreuder/archive/2006/07/07/Using-section-handlers-to-group-settings-in-the-configuration-file_2E00_.aspx\"相对=nofollow>下面是好的帖子,显示如何做到这一点。
如果您想从比其他的app.config文件中读取值,则需要将其加载到ConfigurationManager中。
试试这个方法:<一href=\"http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration%28v=VS.90%29.aspx\"相对=nofollow> ConfigurationManager.OpenMappedExeConfiguration()
有一个如何的MSDN文章中使用的例子。
I'm working with C#, Framework 3.5 (VS 2008).
I'm using the ConfigurationManager
to load a config (not the default app.config file) into a Configuration object.
Using the Configuration class, I was able to get a ConfigurationSection
, but I could not find a way to get the values of that section.
In the config, the ConfigurationSection
is of type System.Configuration.NameValueSectionHandler
.
For what it worth, when I used the method GetSection
of the ConfigurationManager
(works only when it was on my default app.config file), I received an object type, that I could cast into collection of pairs of key-value, and I just received the value like a Dictionary. I could not do such cast when I received ConfigurationSection
class from the Configuration class however.
EDIT: Example of the config file:
<configuration>
<configSections>
<section name="MyParams"
type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyParams>
<add key="FirstParam" value="One"/>
<add key="SecondParam" value="Two"/>
</MyParams>
</configuration>
Example of the way i was able to use it when it was on app.config (the "GetSection" method is for the default app.config only):
NameValueCollection myParamsCollection =
(NameValueCollection)ConfigurationManager.GetSection("MyParams");
Console.WriteLine(myParamsCollection["FirstParam"]);
Console.WriteLine(myParamsCollection["SecondParam"]);
Here's a good post that shows how to do it.
If you want to read the values from a file other than the app.config, you need to load it into the ConfigurationManager.
Try this method: ConfigurationManager.OpenMappedExeConfiguration()
There's an example of how to use it in the MSDN article.
这篇关于如何获得类型NameValueSectionHandler的配置节的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!