通过配置管理器从 AppSettings 中获取 StringCollection

2024-03-21

我正在像这样访问程序集的配置:

ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
AppSettingsSection appSettings = conf.AppSettings;

我的 .config 文件包含这样的部分

<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="CsDll.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<connectionStrings>
    <add name="CsDll.Properties.Settings.SabreCAD" connectionString="A Connection string." />
    <add name="CsDll.Properties.Settings.StpParts" connectionString="Another connection string" />
</connectionStrings>
 <applicationSettings>
        <CsDll.Properties.Settings>
            <setting name="StpInsertSearchPath" serializeAs="Xml">
                <value>
                    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <string>A string</string>
                        <string>Another string in the collection</string>

如果编辑 .config 文件,我可以成功读取连接字符串(包括更改)。所以,我知道我已连接到正确的文件。但我在 appSettings 对象内部找不到该字符串集合。它不在 .Settings KeyValueConfigurationCollection 中。在哪里可以找到我的弦乐收藏?


您应该使用这种更简单的语法来访问集合中的项目

foreach (string s in CsDll.Properties.Settings.Default.StpInsertSearchPath)
{
    Console.WriteLine(s);
}

EDIT:

下面的代码应该可以解决问题

ExeConfigurationFileMap map = new ExeConfigurationFileMap(); 
map.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config"; 
Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
ConfigurationSectionGroup appSettingsGroup = conf.GetSectionGroup("applicationSettings");
ClientSettingsSection clientSettings = (ClientSettingsSection) appSettingsGroup.Sections["CsDll.Properties.Settings"];
ConfigurationElement element = clientSettings.Settings.Get("StpInsertSearchPath");
string xml = ((SettingElement)element).Value.ValueXml.InnerXml;
XmlSerializer xs = new XmlSerializer(typeof(string[]));
string[] strings = (string[])xs.Deserialize(new XmlTextReader(xml, XmlNodeType.Element, null));
foreach (string s in strings)
{
    Console.WriteLine(s);
}

可能有更短的方法,但这对我有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过配置管理器从 AppSettings 中获取 StringCollection 的相关文章

随机推荐

  • 现代 Unix/Linux 系统上的密码是否仍限制为 8 个字符?

    多年前 Unix 密码的长度限制为 8 个字符 或者如果密码长度超过 8 个字符 那么多余的字符也不会产生任何影响 大多数现代 Unix Linux 系统上仍然是这种情况吗 如果是这样 大约什么时候在大多数系统上可以使用更长的密码 有没有一
  • 将双精度数字舍入为以位数给定的较低精度的有效方法

    在 C 中 我想将双精度舍入到较低的精度 以便可以将它们存储在关联数组中不同大小的存储桶中 与通常的舍入不同 我想舍入到多个有效位 因此 大数字的绝对变化将比小数字变化大得多 但它们往往会按比例变化 因此 如果我想四舍五入到 10 个二进制
  • 为什么这个具有推导返回类型的内联方法尚未定义?

    include
  • RadGrid 底部的水平滚动空白

    我正在使用 RadGrid 从数据库检索数据 我的 RadGrid 中有更多列 因此我需要显示 RadGrid 水平滚动以防止页面扩展 但禁用垂直滚动 因此网格的高度应扩展以始终显示网格中的所有行 我得到了结果 但 RadGrid 底部有空
  • 使用 Chosen 链接选择

    我正在尝试将选择与Chosen https github com harvesthq chosen and Chained http www appelsiini net projects chained但我不确定我是否正确实现了 chos
  • 文件观察器创建事件

    我正在使用 net 文件监视程序监视文件夹中的某些类型的文件 mbxml 我正在使用 filewatcher 创建的事件 一旦创建的事件触发 我必须将此文件移动到另一个文件夹 这种方法的问题在于 一旦文件复制开始 就会触发创建的事件 因此
  • 存储用户时区的最佳实践 - TSQL/.Net

    我需要跟踪用户的时区 以便在他们指定的特定时间 在他们自己的时区 处理他们的信息 或不处理 显而易见的答案是将时区及其个人资料信息存储在用户数据库中 有点棘手的是夏令时 从下图中请注意 大多数北部和南部地区使用夏令时偏移 因此 存储时区偏移
  • 防止 PHP 脚本在运行时耗尽所有资源?

    我有一个每日 cron 作业 运行大约需要 5 分钟 它会收集一些数据 然后更新各种数据库 它工作正常 但问题是 在这 5 分钟内 该站点完全没有响应任何请求 无论是 HTTP 还是其他请求 看起来 cron 作业脚本在运行时会占用所有资源
  • 使用基数排序实现 std::sort 重载是否合法?

    对于适用的数据类型 良好的基数排序可以大幅击败比较排序 但是std sort通常作为 introsort 实现 有没有理由不使用基数排序来实现std sort 基数排序不足以实现std sort因为std sort仅要求类型具有可比性 但对
  • Flutter:固定高度容器内的可滚动列子项

    我有一些容器里面一个ListView这将导致可滚动内容在一个页面内 每个容器都有一个 Column 作为子容器在列中 我有一个标题和一个分隔线 然后是实际内容 我希望其中一个容器是这样的 Title divider Scrollable c
  • Windows8:设备标识符

    我目前正在尝试检索唯一的设备标识符 这是我的代码 var token Windows System Profile HardwareIdentification getPackageSpecificToken null var reader
  • 如何在新页面上显示 AJAX 响应

    我正在phonegap中开发移动应用程序并使用intel xdk 我想在新的html页面上显示ajax响应我在google上搜索并找到了这个解决方案window open 但这种方法对我不起作用并显示空白 白屏 我想显示我的数据search
  • pyqt中GUI的模型视图实现错误

    当我关闭应用程序时 以下示例代码因此错误而崩溃 QBasicTimer start QBasicTimer can only be used with threads started with QThread 这是我的代码 import s
  • 如何使用 Modelform 和 jquery 在 django 中获取相互依赖的下拉菜单?

    我是 django 和 jquery 的新手 我正在开发一个基于 django 的应用程序 其中表单中有 3 个下拉列表 1 校园 2 学校 3 中心 层次结构是校园有学校 学校有中心 我想将这些下拉菜单相互链接 例如 我有 3 个校区 即
  • 不同分区中的 COM+ 对象激活

    我创建了一个 COM 域分区 然后将其映射到 Windows 2008 服务器计算机 并将 COM 应用程序导入其中 我尝试使用以下 C 代码远程激活服务器上特定分区中的对象 partition guid Guid guidMyPartit
  • Spring Data JPA - 多对多查询

    我有两个实体 人物 和 电影 Entity public class Person some fields ManyToMany fetch FetchType LAZY mappedBy actors OrderBy id private
  • React Jest/Enzyme 测试:useHistory Hook 破坏测试

    我对 React 还很陌生 所以请原谅我的无知 我有一个组件 const Login FunctionComponent gt const history useHistory extra logic that probably not n
  • Pandas groupby 自定义组

    假设我有一个像这样的数据框 df pd DataFrame A 1 2 3 4 5 6 B a a b b c c print df A B 0 1 a 1 2 a 2 3 b 3 4 b 4 5 c 5 6 c 如何按列分组B使得这些组是
  • 未使用 c# 在 Windows 中设置环境。我哪里出错了?

    string path System Environment GetEnvironmentVariable Path Console WriteLine path if path Contains C ccstg if path EndsW
  • 通过配置管理器从 AppSettings 中获取 StringCollection

    我正在像这样访问程序集的配置 ExeConfigurationFileMap map new ExeConfigurationFileMap map ExeConfigFilename Assembly GetExecutingAssemb