reading content of an config file into the dll ass

2019-07-15 18:18发布

I have saved strings in a dll application's setting. I want to retireve them.

Here is the configuration file for my 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>

It resides in the same directory as my dll. It is called: Search.dll.config My dll is called: Search.dll

I want to read the values of ReadIndex and WriteIndex from this config file into my dll.

Here is the code:

    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");

The code works up to getting the last line here. But how do I get the settings strings?

Yes I can use cs.SectionInformation.GetRawXml(); to get the xml and then interrogate it to get the values, but that is a kluge.
How do I read the values? Preferably into a Settings object? Many thanks!

2条回答
Fickle 薄情
2楼-- · 2019-07-15 18:20
<?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();
查看更多
Deceive 欺骗
3楼-- · 2019-07-15 18:35

you must add tag "appSettings" into tag "configuration" in your file "app.config" in visual studio

like the bellow:

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

and then use this bellow code in c#

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

if you want to update your configuration you can open the "Search.dll.config" file and then update it.

please refer to the bellow answer:

Reading dll.config (not app.config!) from a plugin module

查看更多
登录 后发表回答