将配置文件的内容读入与其关联的dll中

3
我已经在一个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部分——名称看起来有点不对,此外,当您从变量bbb(或ccc)获取值时,可能只需要添加.ToString()。 - Jeremy Thompson
@JeremyThompson,没有错误信息。我只是无法获取存储在配置文件中的值。bbb和ccc现在返回null。因此,在null上添加“.ToString()”将导致空引用异常。 - Barka
我强烈感觉通过观看http://www.youtube.com/watch?v=juBDM3fb-i0上的视频,你可以得到所有需要的帮助。 - Nisarg Shah
2个回答

3
<?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();

谢谢!我的整个方法都是错误的。ASP.NET将dll放在C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root......目录中,而配置文件不会被复制到那里。但由于我在帖子中没有提到ASP.NET,所以我将您的帖子标记为答案,并在新帖子中重写问题。 - Barka

0

在 Visual Studio 中,您必须将标签 "appSettings" 添加到文件 "app.config" 的 "configuration" 标签中。

示例如下:

<configuration>
    <appSettings>
        <add key="ReadIndex" value="aaa"/>
        <add key="WriteIndex" value="111"/>
    </appSettings>
</configuration>

然后在C#中使用下面的代码

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string userName = appConfig.AppSettings.Settings["ReadIndex"].Value;
string password = appConfig.AppSettings.Settings["WriteIndex"].Value;

如果您想更新配置,可以打开“Search.dll.config”文件,然后进行更新。

请参考下面的答案:

从插件模块读取dll.config(而不是app.config!)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接