Using app.config from both the exe and a dll


A

Anders Eriksson

I have an application, DCMarker.exe, that uses app.config for some
configuration. I have created a class, Config.cs, that contains all the
configuration and I have this method to get the values out of app.config

private void InitConfig()
{
this.imagePath = Properties.Settings.Default.imagePath;
this.jobfilePath = Properties.Settings.Default.jobfilePath;
this.templatePath = Properties.Settings.Default.templatePath;
this.projectPath = Properties.Settings.Default.projectPath;
this.axisXEnable = Properties.Settings.Default.axisXEnable;
this.axisYEnable = Properties.Settings.Default.axisYEnable;
this.axisZEnable = Properties.Settings.Default.axisZEnable;
this.axisREnable = Properties.Settings.Default.axisREnable;
}

This works!

Now I want to use the same configuration in a dll that the application uses.

The application has namespace: DCMarker
and the dll has namespace: DCMarkerLib

The app.config is created using the Settings tab in the Properties for
the application. See below for xml code.


How should I do this so that I can use the same app.config file in both
the exe and the dll?

// Anders
--
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="DCMarker.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="DCMarker.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<DCMarker.Properties.Settings>
<setting name="test" serializeAs="String">
<value>Hello world!</value>
</setting>
</DCMarker.Properties.Settings>
</userSettings>
<applicationSettings>
<DCMarker.Properties.Settings>
<setting name="jobfilePath" serializeAs="String">
<value>C:\jobfiles\</value>
</setting>
<setting name="imagePath" serializeAs="String">
<value>c:\bilder\</value>
</setting>
<setting name="templatePath" serializeAs="String">
<value>c:\bilder\</value>
</setting>
<setting name="projectPath" serializeAs="String">
<value>c:\prj\</value>
</setting>
<setting name="axisXEnable" serializeAs="String">
<value>0</value>
</setting>
<setting name="axisYEnable" serializeAs="String">
<value>0</value>
</setting>
<setting name="axisZEnable" serializeAs="String">
<value>0</value>
</setting>
<setting name="axisREnable" serializeAs="String">
<value>0</value>
</setting>
</DCMarker.Properties.Settings>
</applicationSettings>
</configuration>
 
Ad

Advertisements

A

Arne Vajhøj

I have an application, DCMarker.exe, that uses app.config for some
configuration. I have created a class, Config.cs, that contains all the
configuration and I have this method to get the values out of app.config

private void InitConfig()
{
this.imagePath = Properties.Settings.Default.imagePath;
this.jobfilePath = Properties.Settings.Default.jobfilePath;
this.templatePath = Properties.Settings.Default.templatePath;
this.projectPath = Properties.Settings.Default.projectPath;
this.axisXEnable = Properties.Settings.Default.axisXEnable;
this.axisYEnable = Properties.Settings.Default.axisYEnable;
this.axisZEnable = Properties.Settings.Default.axisZEnable;
this.axisREnable = Properties.Settings.Default.axisREnable;
}

This works!

Now I want to use the same configuration in a dll that the application
uses.

The application has namespace: DCMarker
and the dll has namespace: DCMarkerLib

The app.config is created using the Settings tab in the Properties for
the application. See below for xml code.

How should I do this so that I can use the same app.config file in both
the exe and the dll?

If the DLL is being used from the EXE, then in theory you could
access it the exact same way.

But that would create a very bad circular dependency between
EXE and DLL.

The nice solution is to read the stuff in code in the EXE and
pass it to code in the DLL.

That would also solve the problem if the DLL is being
used in a different context.

Arne
 
A

Anders Eriksson

The nice solution is to read the stuff in code in the EXE and
pass it to code in the DLL.

The logical way is to create a class that both the exe and the dll uses.
But since there are two different namespaces How do I solve this?

Do I create a new namespace for just the config class or...

// Anders
 
A

Arne Vajhøj

The logical way is to create a class that both the exe and the dll uses.
But since there are two different namespaces How do I solve this?

Do I create a new namespace for just the config class or...

The namespace does not matter. You can put in in any of two existing
namespaces or in a new namespace as you see most appropriate.

What matters is in what assembly you put it in. I would
assume that it would need to either be in the existing DLL or in
a another DLL to avoid a circular reference between the EXE and
the existing DLL.

And I still think that the actual reading should be done
in the EXE, because it is the EXE's config file.

Arne
 
J

Jason Keats

Anders said:
The app.config is created using the Settings tab in the Properties for
the application. See below for xml code.

I usually just use:
System.Configuration.ConfigurationManager.AppSettings
 
A

Arne Vajhøj

I usually just use:
System.Configuration.ConfigurationManager.AppSettings

Me too. And in this context it would be a lot easier to
understand because that method does not rely on some
generated code.

Arne
 
Ad

Advertisements

A

Anders Eriksson

I usually just use:
System.Configuration.ConfigurationManager.AppSettings

I tried that but it didn't find anything. Just returned null...

this.imagePath =
System.Configuration.ConfigurationManager.AppSettings["imagePath"].ToString();

Here I get an NullReference Exception, since
System.Configuration.ConfigurationManager.AppSettings["imagePath"]
returns null and then I do .ToString();

I rather would use this, but I can't figure out why it doesn't find the
values...

// Anders
 
A

Anders Eriksson

What matters is in what assembly you put it in. I would
assume that it would need to either be in the existing DLL or in
a another DLL to avoid a circular reference between the EXE and
the existing DLL.

And I still think that the actual reading should be done
in the EXE, because it is the EXE's config file.

Well I ended (so far anyway) up with putting the class in the DLL and
the actual reading in the EXE. Made the class a singleton, which worked
very well.

// Anders
 
J

Jason Keats

Anders said:
I usually just use:
System.Configuration.ConfigurationManager.AppSettings

I tried that but it didn't find anything. Just returned null...

this.imagePath =
System.Configuration.ConfigurationManager.AppSettings["imagePath"].ToString();


Here I get an NullReference Exception, since
System.Configuration.ConfigurationManager.AppSettings["imagePath"]
returns null and then I do .ToString();

I rather would use this, but I can't figure out why it doesn't find the
values...

// Anders

Perhaps you forgot to modify your app.config file. For example...

<configuration>
<appSettings>
<add key="imagePath" value="whatever"/>
</appSettings>
</configuration>

string imagePath = ConfigurationManager.AppSettings["imagePath"];
 
A

Anders Eriksson

Perhaps you forgot to modify your app.config file. For example...

<configuration>
<appSettings>
<add key="imagePath" value="whatever"/>
</appSettings>
</configuration>

string imagePath = ConfigurationManager.AppSettings["imagePath"];

This would then mean that it's impossible to use the Property editor.

Seem strange that Microsoft should make such a decision.

OK, Thank you (all) for the help!

// Anders
 
Ad

Advertisements

A

Arne Vajhøj

Perhaps you forgot to modify your app.config file. For example...

<configuration>
<appSettings>
<add key="imagePath" value="whatever"/>
</appSettings>
</configuration>

string imagePath = ConfigurationManager.AppSettings["imagePath"];

This would then mean that it's impossible to use the Property editor.

Seem strange that Microsoft should make such a decision.

It all depends on the scenario.

The app setting is a lot easier to edit with a plain editor.

In many cases that is a good thing, because that allow the
operations people to change the configuration as needed (they
will hopefully not have VS installed on a production system).

Arne
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top