I want to add a custom configuration section to my App.Config file in my C# 2010 project. I want the section to look like the following.

<DmModules>
      <Module Name="Module1">
            <add key="DbType" value="...">
            <add key="ConnType" value="...">
      </Module>
      <Module Name="Module2">
            <add key="DbType" value="...">
            <add key="ConnType" value="...">
      </Module>
</DmModules>

Of course there can be as many <Module> elements as needed, and each <Module> must contain two keys as specified.

Note : DbType and ConnType doesn't necessarily have to be "key", but even something like the following would do as long as I can read that value later from another part of my program.

<Module Name="Module1">
      <DbType Name="..."/>
      <ConnType Name="..."/>
</Module>

So as said in the above note, I need to be able to read these values later from inside my program and use the values. How can it be done?

Recommended Answers

All 12 Replies

I tired the first, but it gives an error as follows.

Could not read the type DB_APPROACH_3.PageAppearanceSection from the assembly System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.

Note : DB_APPROACH_3 is my namespace, and according to the example DB_APPROACH_3 = Samples.AspNet

Any ideas?

I should have said "Exception", not error.

And the exception is given at the first line of code when I try to read it, which is the following line.

DB_APPROACH_3.PageAppearanceSection config = (DB_APPROACH_3.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection("pageAppearanceGroup/pageAppearance");

No Idea let me check have you checked the second link too?

is there exists the section which you are trying to access in this code where exception raised?

is there exists the section which you are trying to access in this code where exception raised?

Sorry I did not understand your question. Can you please elaborate?

I want to ask in this code

DB_APPROACH_3.PageAppearanceSection config = (DB_APPROACH_3.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection("pageAppearanceGroup/pageAppearance");

you are getting the section "pageAppearanceGroup/pageAppearance" have you checked it in you file is it present there?

I want to ask in this code

DB_APPROACH_3.PageAppearanceSection config = (DB_APPROACH_3.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection("pageAppearanceGroup/pageAppearance");

you are getting the section "pageAppearanceGroup/pageAppearance" have you checked it in you file is it present there?

First of all, I made a silly mistake, because I put that line of code which generate the exception in a different project. I rectified it, now I do not get the exception.
However, now, the config object of type DB_APPROACH_3.PageAppearanceSection does not receive any value after that line of code is executed. It remains "null".

To answer your question, yes that line IS there. I just copied and pasted (contents of App.Config file) from Link1 you provided and changed appropriately. This is how my App.Config looks like now.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<!-- Configuration section-handler declaration area. -->
	<configSections>
		<sectionGroup name="pageAppearanceGroup">
			<section
			  name="pageAppearance"
			  type="DB_APPROACH_3.PageAppearanceSection"
			  allowLocation="true"
			  allowDefinition="Everywhere"
			  />
		</sectionGroup>
		<!-- Other <section> and <sectionGroup> elements. -->
	</configSections>

	<!-- Configuration section settings area. -->

	<!-- Configuration section-handler declaration area. -->

	<!-- Configuration section settings area. -->
	<pageAppearanceGroup>
		<pageAppearance remoteOnly="true">
			<font name="TimesNewRoman" size="18"/>
			<color background="000000" foreground="FFFFFF"/>
		</pageAppearance>
	</pageAppearanceGroup>

	<!-- Other configuration settings, such as system.web -->
</configuration>

Have you checked the second link provided by abellazm? it has a more detailed example with source code

Have you checked the second link provided by abellazm? it has a more detailed example with source code

Yes, but I am kind of new to this thing and is finding it hard to wrap my brains around that articles. I'm trying slowly to understand it but no luck so far.

what's the exception you are facing now?

commented: Great helper! +26

what's the exception you are facing now?

Okay, first I understood one thing which is pretty weird.

Let us say I have a Windows Console Application, called ConfigTestApp.
I have a DLL named CustomConfig, in which I implement all the code above mentioned, and it also contain the App.Config file.
I also have a static method inside a class in CustomConfig which merely returns an object of type PageAppearanceSection as follows.

public static PageAppearanceSection GetValue()
{
         PageAppearanceSection config =
         (CustomConfig.PageAppearanceSection)System.Configuration.ConfigurationManager.GetSection(
        "pageAppearanceGroup/pageAppearance");

         return config
}

Then I call the above GetValue() method FROM ConfigTestApp.

But it returns NULL, because for some weird reason Windows access the AppConfig of whoever runs the DLL, not the AppConfig of the DLL. Since ConfigTestApp does not have an AppConfig, it returns null.

So I copied the same AppConfig to ConfigTestApp, and now it gives the same Exception I mentioned above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.