3

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!

Barka
  • 8,764
  • 15
  • 64
  • 91
  • Whats the error message? It looks like the code should work but I dont think you are reading the XML section properly - the names look a little off, also you may just need to add .ToString() when you get the values from the var bbb (or ccc) – Jeremy Thompson Mar 19 '12 at 00:36
  • @JeremyThompson, there is no error message. I am just unable to get to the values stored in the config file. bbb and ccc are now returning null. So adding '.ToString()' on a null would cause a null reference exception. – Barka Mar 19 '12 at 00:51
  • I have a strong feeling that you can get all the help you need by viewing the video at http://www.youtube.com/watch?v=juBDM3fb-i0 – Nisarg Shah Jan 07 '14 at 10:45

2 Answers2

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();
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thanks! My whole approach is wrong. ASP.NET puts the dlls in a directory at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\...... and the config file doesnt get copied there. But since I didn't mention ASP.NET in my post, I am marking your post as answer and rewriting the problem in a new post. – Barka Mar 21 '12 at 18:14
0

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

Community
  • 1
  • 1
Mohamad Chami
  • 1,226
  • 14
  • 10