c# - 将配置文件的内容读入与之关联的 dll

标签 c# .net dll web-config app-config

我已将字符串保存在 dll 应用程序的设置中。我想让他们退休。

这是我的 dll 的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxxxxxxx" >
            <section name="Search.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <PishiSearch.Properties.Settings>
            <setting name="ReadIndex" serializeAs="String">
                <value>C:\Index</value>
            </setting>
            <setting name="WriteIndex" serializeAs="String">
                <value>C:\WriteIndex</value>
            </setting>
        </PishiSearch.Properties.Settings>
    </applicationSettings>
</configuration>

它与我的 dll 位于同一目录中。它被称为:Search.dll.config 我的dll叫做:Search.dll

我想从这个配置文件中读取 ReadIndex 和 WriteIndex 的值到我的 dll 中。

代码如下:

    var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
    var config = ConfigurationManager.OpenExeConfiguration(location);
    var sections = config.Sections; //count of this is 21
    ConfigurationSectionGroup csg = config.GetSectionGroup("applicationSettings");
    ConfigurationSectionCollection csc = csg.Sections;
    ConfigurationSection cs = csc.Get("Search.Properties.Settings");

代码一直运行到这里的最后一行。但是我如何获得设置字符串呢?

是的,我可以使用 cs.SectionInformation.GetRawXml(); 获取 xml,然后查询它以获取值,但这是一个问题。
我如何读取这些值?最好是设置对象?非常感谢!

最佳答案

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <applicationSettings>

    </applicationSettings>
    <appSettings>
        <add key="ReadIndex" value="C:\Index"/>
    </appSettings>
</configuration>


var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
var config = ConfigurationManager.OpenExeConfiguration(location);
var sections = config.Sections; //count of this is 21
string s = config.AppSettings.Settings["ReadIndex"].Value.ToString();

关于c# - 将配置文件的内容读入与之关联的 dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9763637/

相关文章:

c# - 无法从弹性客户端搜索响应中获取_source字典键值

c# - 如何在 Windows Phone/XNA 上使 flick Gesture 成为平滑的 Scroll

c# - 简单的 LINQ 重构/样式

c# - C# Convert.ToDateTIme 函数将日期读取为 "dd/mm/yyyy"还是 "mm/dd/yyyy"?

c++ - 编写库时是否应将可见性/导出宏应用于模板?

c dll 的 C# .net 包装器,特别是 lglcd (g19 sdk)

c# - ria 服务服务器端数据结构 : is it shared between threads

c# - 您能否指定如何解析属性中的参数?

javascript - 当 javascript 值插入到 .net 文本框时,document.getElementsByName(...)[0] 未定义

c++ - 将成熟的应用程序转换为 DLL