gpt4 book ai didi

c# - app.config 中的部分类型

转载 作者:行者123 更新时间:2023-12-05 01:04:26 24 4
gpt4 key购买 nike

我正在学习如何在 app.config 的 configsection 中创建节组和节。
所以,让我们来个样本

    <configSections>
<sectionGroup name="trackGroup">
<section name="trackInfo" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<trackGroup>
<trackInfo>
<add key = "ASHUM" value="ASH-UM"/>
</trackInfo>
</trackGroup>

所以现在当我浏览各种文章时,我发现每个人都使用不同类型的部分
所以,我在这里找到了不同的类型:
http://msdn.microsoft.com/en-us/library/aa309408%28v=vs.71%29.aspx

但是这里没有提到我使用的类型。
我从一个随机示例中得到了这种类型,据我所知,它实际上是在定义 appsetting 部分的设置。
有人可以帮我我的样本中的类型是什么意思吗?我的意思是什么是版本,公共(public) token ,文化我们如何定义这些?
另外我想知道哪种类型更好用?
就像我必须在运行时访问这些设置,甚至在运行时修改一些。

另外我想这些不同的类型有不同的方式来访问它们?
就像上面我的示例中的情况一样,我正在访问键和值:
  static void getFull(string sectionName)
{
System.Collections.Specialized.NameValueCollection section = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection(sectionName);
if (section != null)
{
foreach (string key in section.AllKeys)
{
Console.WriteLine(key + ": " + section[key]);
}
}
}

但是,如果我们使用 MSDN 链接中提供的类型,我将如何访问键和值?

最佳答案

这是我为创建配置部分所做的事情。它还允许外部化部分。

应用程序配置

<?xml version="1.0"?>
<configuration>
<configSections>
<section name="TrackInfo" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>

<TrackInfo configSource="TrackInfo.config"/>
</configuration>

TrackInfo.config
<?xml version="1.0" encoding="utf-8" ?>
<TrackInfo>
<add key="SOME_KEY" value="SOME_VALUE" />
</TrackInfo>

C#
NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection( "TrackInfo" );
if( null == section ) throw new Exception( "Missing TrackInfo.config" );

string val = section["SOME_KEY"];

关于c# - app.config 中的部分类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23160500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com