gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:30:14 25 4
gpt4 key购买 nike

我已将字符串保存在 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/

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