C# 从App.Config文件上的configSections获取节名
我想知道一种获取App.Config文件中标记configSections中所有节名的方法 到目前为止,我发现的是这样的:C# 从App.Config文件上的configSections获取节名,c#,.net,C#,.net,我想知道一种获取App.Config文件中标记configSections中所有节名的方法 到目前为止,我发现的是这样的: var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var localSections = cfg.Sections.Cast<ConfigurationSection>().Where(s => s.SectionInformation.IsD
var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections = cfg.Sections.Cast<ConfigurationSection>().Where(s => s.SectionInformation.IsDeclared);
var cfg=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections=cfg.Sections.Cast(),其中(s=>s.SectionInformation.IsDeclared);
但由于某些原因,localSections总是显示为null。
下面是我的app.config文件的外观:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="frequency" value=""/>
</appSettings>
<configSections>
<section name="Name1" type="System.Configuration.NameValueSectionHandler" />
<section name="Name2" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<Name1>
<add key="active" value="on"/>
<add key="repeat" value="off"/>
</Name1>
<Name2>
<add key="active" value="on"/>
<add key="repeat" value="off"/>
</Name2>
</configuration>
您可以通过节信息获取节名
string s;
var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections = cfg .Sections.Cast<ConfigurationSection>().Where(s => s.SectionInformation.IsDeclared);
foreach( var i in localSections)
s = i.SectionInformation.SectionName;
字符串s;
var cfg=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections=cfg.Sections.Cast(),其中(s=>s.SectionInformation.IsDeclared);
foreach(localSections中的var i)
s=i.SectionInformation.SectionName;
您可以通过节信息获取节名
string s;
var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections = cfg .Sections.Cast<ConfigurationSection>().Where(s => s.SectionInformation.IsDeclared);
foreach( var i in localSections)
s = i.SectionInformation.SectionName;
字符串s;
var cfg=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections=cfg.Sections.Cast(),其中(s=>s.SectionInformation.IsDeclared);
foreach(localSections中的var i)
s=i.SectionInformation.SectionName;
您可以通过节信息获取节名
string s;
var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections = cfg .Sections.Cast<ConfigurationSection>().Where(s => s.SectionInformation.IsDeclared);
foreach( var i in localSections)
s = i.SectionInformation.SectionName;
字符串s;
var cfg=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections=cfg.Sections.Cast(),其中(s=>s.SectionInformation.IsDeclared);
foreach(localSections中的var i)
s=i.SectionInformation.SectionName;
您可以通过节信息获取节名
string s;
var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections = cfg .Sections.Cast<ConfigurationSection>().Where(s => s.SectionInformation.IsDeclared);
foreach( var i in localSections)
s = i.SectionInformation.SectionName;
字符串s;
var cfg=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var localSections=cfg.Sections.Cast(),其中(s=>s.SectionInformation.IsDeclared);
foreach(localSections中的var i)
s=i.SectionInformation.SectionName;
这并不难,这里有一个链接,我可以通过一次谷歌搜索快速找到,如果没有帮助,请尝试自己进行谷歌搜索。。不是那样的Trivial@MethodMan谢谢你的评论!但这不是我想要的。。。我可以通过知道节的名称轻松检索节。但在这种情况下,我需要首先从“configSections”中的每个部分获取属性“name”,这意味着只有“Name1”和“Name2”。这并不是很难。这里有一个链接,我可以通过一次谷歌搜索为您快速找到,如果没有帮助,请尝试自己进行谷歌搜索。。不是那样的Trivial@MethodMan谢谢你的评论!但这不是我想要的。。。我可以通过知道节的名称轻松检索节。但在这种情况下,我需要首先从“configSections”中的每个部分获取属性“name”,这意味着只有“Name1”和“Name2”。这并不是很难。这里有一个链接,我可以通过一次谷歌搜索为您快速找到,如果没有帮助,请尝试自己进行谷歌搜索。。不是那样的Trivial@MethodMan谢谢你的评论!但这不是我想要的。。。我可以通过知道节的名称轻松检索节。但在这种情况下,我需要首先从“configSections”中的每个部分获取属性“name”,这意味着只有“Name1”和“Name2”。这并不是很难。这里有一个链接,我可以通过一次谷歌搜索为您快速找到,如果没有帮助,请尝试自己进行谷歌搜索。。不是那样的Trivial@MethodMan谢谢你的评论!但这不是我想要的。。。我可以通过知道节的名称轻松检索节。但在本例中,我需要首先从“configSections”中的每个部分获取属性“name”,这意味着只有“Name1”和“Name2”。