properties.settings from other project

  • Thread starter Thread starter SimonV
  • Start date Start date
S

SimonV

Hi,
How can I use a connectionstring (or an other setting) that's set in an
other project in the same solution? I've already referenced the other
project .Properties, but that doesn't seem to be sufficent.
I'm using c# 2005
 
You cannot access settings from another project directly because the class
Settings is declared as internal. However you can write a wapper like this:

Given a class library project called "Library":

namespace Library
{
public class LibrarySettings
{
public object this[string property]
{
get
{
return Properties.Settings.Default[property];
}
set
{
Properties.Settings.Default[property] = value;
}
}
}
}

To make the settings to work in a multiproject solution, you must merge the
app.config files into the app.config file of the executable project.

Suppose your executable project is WinApp (a windows application). The
app.config file for WinApp should look like this:

<?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="WinApp.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="Library.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<WinApp.Properties.Settings>
<setting name="FormTitle" serializeAs="String">
<value>Form1</value>
</setting>
</WinApp.Properties.Settings>
<Library.Properties.Settings>
<setting name="Setting1" serializeAs="String">
<value>SomeValue</value>
</setting>
</Library.Properties.Settings>

</userSettings>
<connectionStrings>
<add name="Library.Properties.Settings.ConnectionString1"
connectionString="Data Source=(local);Initial Catalog=Norhtwind;Integrated
Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

I tested this in a form, and it works:

private void button1_Click(object sender, EventArgs e)
{
Library.LibrarySettings settings = new Library.LibrarySettings();
MessageBox.Show(settings["ConnectionString1"].ToString());
}

This aproach is very owfull, however I have not found another more ellegant,
sorry. If someone have a more ellegant aproach, please let me know.

Regards from Madrid (Spain)

Jesús López
VB MVP
 
Hmm, strange:
When I create a windows project, an other class project can read the
windows project connectionString (not a string setting).
But when I create a VSTO project and I add an connectionString. The
class project can't find that connectionString.
How does this come?
 
Back
Top